Defines an endpoint - a connection point between the client side of a component and its server side. For example:
endpoint :do_something do |params| # ... end
By defining the endpoint on the server, the client side automatically gets a method that is used to call the server, in this case `doSomething` (note conversion from underscore to camelcase). It can be called like this:
this.doSomething(argsObject, callbackFunction, scope);
argsObject is what the server side will receive as the params argument
callbackFunction (optional) will be called after the server successfully processes the request
scope (optional) the scope in which callbackFunction will be called
The callback function may receive an argument which will be set to the value that the server passes to the special set_result key in the resulting hash:
endpoint :do_something do |params|
# ...
{:set_result => 42}
end
Any other key in the resulting hash will result in a corresponding JavaScript-side function call, with the parameter set to the value of that key. For example:
endpoint :do_something do |params|
# ...
{:set_result => 42, :set_title => "New title, set by the server"}
end
This will result in the call to the setTitle method on the client side of the component, with “New title, set by the server” as the parameter.
# File lib/netzke/services.rb, line 36 def endpoint(name, options = {}, &block) register_endpoint(name, options) define_method("#{name}_endpoint", &block) if block # if no block is given, the method is supposed to be defined elsewhere # define_method name, &block if block # if no block is given, the method is supposed to be defined elsewhere define_method :"_#{name}_ep_wrapper" do |*args| res = send("#{name}_endpoint", *args) res.respond_to?(:to_nifty_json) && res.to_nifty_json || "" end end
Registers an endpoint
# File lib/netzke/services.rb, line 59 def register_endpoint(ep, options) current_endpoints = read_inheritable_attribute(:endpoints) || {} current_endpoints.merge!(ep => options) write_inheritable_attribute(:endpoints, current_endpoints) end
Generated with the Darkfish Rdoc Generator 2.