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.

Post Information

Tags:

We're Reading

Feeds/Syndication

2 Responses to “[ANN] ActiveJax”

Leave a Reply