ActiveJax is a RoR plugin. It’s goal is to be an effective ActiveRecord -> Prototypejs bridge, allowing you to directly call your finders from javascript. Alright let’s see some code:
Ruby Model:
class Author < ActiveRecord::Base
active_jax
def find_by_name(n)
find(:all, :conditions => {:name => n})
end
end
Java Script
ActiveJax.Author.find_by_name("Nicholas Schlueter").
each(function(author) {
alert(author.name);
});
As you can see you just call the finder by name in the javascript.
It can also do belongs_to associations:
Ruby Model:
class Author < ActiveRecord::Base
active_jax :include => :publisher
belongs_to :publisher
def find_by_name(n)
find(:all, :conditions => {:name => n})
end
end
Java Script
ActiveJax.Author.find_by_name("Nicholas Schlueter").
each(function(author) {
alert(author.publisher.name);
});
Hold on, isn’t that pretty dangerous. It’s true, exposing your model can be dangerous. There is some security built in. Building on our previous example:
Ruby Model:
class Author < ActiveRecord::Base
active_jax :include => :publisher, :excluded_columns => :email
belongs_to :publisher
def find_by_name(n)
find(:all, :conditions => {:name => n})
end
end
Java Script
ActiveJax.Author.find_by_name("Nicholas Schlueter").
each(function(author) {
alert(author.publisher.name);
});
Hurray! The email address is no longer published to all would be spammers (bastards).
In order for all this to work you have to do 1 more thing, include the following somewhere in your html doc:
<%= active\_jax\_include %>
There is also a way to enable controller actions, but to be honest it is only partially thought through.
Anyone interested in learning more is encouraged to download the ActiveJax Sample Application and/or install the plugin and try it out.
./script/plugin install http://svn.simpltry.com/plugins/active_jax
In the future more information will be available at [http://rails.simpltry.com/active_jax.html](http://rails.simpltry.com/active_jax.html). Please be aware this isn’t production tested or efficient, I will be accepting patches and updating this as needed.
[...] I asked Nicholas how this differs from the existing Jester library developed by Thoughtbot. One significant difference is the syntax. ActiveJax’s mechanisms are all embedded underneath the ActiveJax object, whereas Jester offers up the “models” more directly within JavaScript. ActiveJax also doesn’t depend on the application providing RESTful services, it’s possible to call any method on the models. All this said, the motivations for using Jester versus ActiveJax cross significantly, so it’s worth checking out both libraries if this is an area that interests you. More info is available in this blog post by Nicholas, including a link to a sample application. [...]
Funny how I just now came across this. I’d heard of Jester, but just now in googling did I come across ActiveJax. Cool stuff! Did you tell me about this before? I don’t recall.
Have you made use of this in anything in production? Would be interested in seeing more!