JsOnDemand ========== ### Reqirements ### Rails 1.2+ ### Overview ### This plugin helps quickly create Javascript web services (like Google AJAX Search API). It mixes in a few methods to (hopefully) make your life easier. ### Additions to ActionView::Base ### required_js_on_demand - This is a little Javascript to help create dynamic script tags. It exposes the JavaScript object JsOnDemand. JsOnDemand currently has one Class called Get, here is an example of it's usage: new JsOnDemand.Get(relative_url, callback_function); js_on_demand_get - This class takes one hash. There are two keys that are especially important for creating the JsOnDemand calls: :js_parameters - is a hash that consists of: key - query string parameter name value - javascript variable or literal that contains the value that corresponds to the query string key :callback - is a string that contains the function to be called upon completion. Defaults to "callback" All other keys are passed to AcionView::Base.url_for, to construct the call to the server. Example: If you view looked like this. var GetSomething = function(param1, callback) { <%= js_on_demand_get(:controller => "js", :action => "get_something", :js_parameters => {:server_param => "param1"}) %> }; Would be sent down to the user like: var GetSomething = function(param1, callback) { new JsOnDemand.Get('/js/get_something' + '?' + 'server_param=' + param1, callback); }; Then you end users would just call your new function like this: new GetSomething(1, function(json_data) { //function impl. }); ### Additions to ActionController::Base ### render_js_on_demand(object) - It works in conjunction with required_js_on_demand, and makes sure that the correct callback gets called. The object you pass in will have the to_json method called on it. js_on_demand_callback - Alternatively, as of rails 1.2 there is an options to call: render :json => {"key","value"}.to_json, :callback => js_on_demand_callback which is equivalent to: render_js_on_demand({"key","value"}) ### Aditions to ActionController::AbstractRequest ### js_on_demand? - This returns a boolean indicating whether or not the request was made from the JsOnDemand Object. Example: if request.js_on_demand? render_js_on_demand(some_object) else render :text => some_object.to_json end The subtle difference is that if you use the render_js_on_demand the callback will be called, and if you don't it won't.