Controllers respond to events such as mouseovers, clicks, and form submits. They do this by naming functions, also called actions, with combination css selector and event handlers.
The following are functions that are called directly on the Controller object.
Controller(name, actions) -> NameController
Creates a new Controller class with the provided actions. If the name is 'main' it creates the special MainController class. MainController is used for matching events that are typically associated with the window, document, or body {resize, load, scroll, unload}.
Controller.dispatch(controller, action_name, params) -> action_return_value
Calls the Controller.Action specified by controller and action_name with the given params.
These methods can be called on generated controller classes, i.e. TodosController
GeneratedController.actions() -> [Controller.Action, ... ]
Returns a list of Controller.Actions for this controller.
GeneratedController.className -> String
Returns the small name of the generated controller. For example:
TodosController.className -> 'todos'
controller.continue_to(action) -> Function
Returns a function that when called, calls the action with parameters passed to the function. This is very useful for creating callbacks for Ajax functionality. The callback is called on the same controller instance that created the callback. This allows you to easily pass objects between request and response without resorting to closures. Example:
Controller('todos',{
"a click" : function(params){
this.element = params.element;
this.element.innerHTML = 'deleting ...';
new Ajax.Request('delete', {onComplete: this.continue_to('deleted')}
},
deleted : function(response){
this.element.parentNode.removeChild(this.element);
}
});
controller.redirect_to(options)
Redirects the browser to the target specified in options and adds it to the history.
this.redirect_to({controller: 'todos', action: 'show', id: id})
controller.render(options) -> String
Renders a View template with the controller instance. If action or partial are not supplied in the options, it looks for a view in app/views/controller_name/action_name.ejs
| Option | Default | Description |
|---|---|---|
| action | null | If present, looks for a template in app/views/controller_name/action.ejs |
| partial | null | A string value that looks like: 'folder/template' or 'template'. If a folder is present, it looks for a template in app/views/folder/_template.ejs; otherwise, it looks for a template in app/views/controller_name/_template.ejs. |
| to | null | If present, a HTMLElement or element ID whose text will be replaced by the render. |