Controller

actions, className, continue_to, Controller, Controller.dispatch, render

Related

Controller.Action, Controller.Params

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.

Generator Methods

The following are functions that are called directly on the Controller object.

Controller

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}.

- If a singluar word, this matches the element and elements inside an element with name as its id. If name is plural, it matches actions that start with # to an element with the name as its id, and all other actions to the singlular name.
- A hash of functions.

Controller.dispatch

Controller.dispatch(controller, action_name, params) -> action_return_value

Calls the Controller.Action specified by controller and action_name with the given params.

- The controller class or its shortname (i.e. 'todos').
- The name of the action to be called.
- The params the action will be called with.

Class Methods

These methods can be called on generated controller classes, i.e. TodosController

actions

GeneratedController.actions() -> [Controller.Action, ... ]

Returns a list of Controller.Actions for this controller.

className

GeneratedController.className -> String

Returns the small name of the generated controller. For example:

TodosController.className -> 'todos'

Prototype Methods

continue_to

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);
   }
});
	 

redirect_to

controller.redirect_to(options)

Redirects the browser to the target specified in options and adds it to the history.

- A hash of options. If a controller and action are presented redirect_to directs to that controller/action pair. If controller is not present, it uses the current controller. If action is not present, it defaults to index.
this.redirect_to({controller: 'todos', action: 'show', id: id})

render

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

OptionDefaultDescription
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.