Wouldn’t it be neat that instead of just supporting XmlHTTPRequest we would support a higher level of abstraction on top of XHR and JSON.
This looks like a good fit:
http://orbjson.rubyforge.org/
Maybe some cooperation between the projects?
Here’s some example code:
class PersonController < ApplicationController
def new_person(firstname, lastname)
person = Person.new('firstname' => firstname, 'lastname' => lastname)
person.save
person
end
orbjson_service :new_person
def list_persons
Person.find(:all)
end
orbjson_service :list_persons
def get_person(id)
Person.find_by_id(id)
end
orbjson_service :get_person
def index
# renders index.rhtml as below
end
end
In index.rhtml you then have JavaScript code like:
<pre>
<!-- snip -->
<script language="javascript" src="rails_orbjson.js"/>
<script language="javascript">
controller = connect_to_controller();
// async call as implemented by Orbjson
// will call function given in first argument when data is returned from controller
controller.list_persons(function(persons) {
foreach(var person in persons) {
document.persons.innerHTML += person.first_name;
document.persons.innerHTML += " ";
}
});
Even better, hook it up with some JavaScript templating library like: http://trimpath.com/blog/index.php?p=3
Interesting things to note:
connect_to_controller automatically connects back to the controller that the page came from using sensible defaults?.