Class: Wraith.Controller

Defined in: src/controller.coffee
Inherits: Wraith.BaseView

Overview

The proverbial 'controller' in the MVC pattern. The Controller handles the logic your applications or components may have. This controller handles automatical registration and mapping of models to views.

Examples:

Example markup in HTML.

This will create a SelectList controller
and then create a view using the template with id "ListItem"
mapping to the model list.items (belonging to SelectList)

<ul data-view="SelectList">
  <div data-template="ListItem" data-map="list.items"></div>
</ul>

Instance Method Summary

Inherited Method Summary

Methods inherited from Wraith.BaseView

#constructor, #bindUIEvents, #bindUIEvent, #wrapUIEvent, #handleUIEvent, #unbindUIEvents, #unbindUIEvent, #applyViewUpdate, #updateAttribute

Methods inherited from Wraith.Base

#constructor, #bind, #unbind, #emit, #proxy

Constructor Details

- (void) constructor($el)

Initializes a few private variables and sets the data-id attribute to a uniquely generated id.

Parameters:

  • $el (HTMLElement) The main element to bind the controller to.

Instance Method Details

- (void) init()

Initialize the controller by loading the views and elements with id tags. Also binds UI events to the controllers' respective callbacks

- (void) loadElements()

Load our elements with id attributes into an object that is accesible at the controller level. This makes it easier to access discrete DOM objects within the controller via @els['el-id']

- (void) loadViews()

Find all the views embedded inside the controller and register them with the controller

- (void) registerView($view, twoWay)

Register the view to this controller

Parameters:

  • $view (HTMLElement) The view to be registered

- (Wraith.Model) registerModel(model, as)

Register the model to this controller

Parameters:

  • model (Wraith.Model) The model to register to this controller
  • as (String) The name of the model to register

Returns:

- (void) bindViews(name, model)

Binds any view that is loaded into the controller to the given model.

Parameters:

  • name (String) The name of the model to bind to
  • model (Wraith.Model) The model to map the namespace to

- (void) bindView(model, binding, view, twoWay)

Binds a single instance of a view to a model. Usings binding as the dot notation map.

Parameters:

  • model (Wraith.Model) The model to bind to
  • binding (String) The dot notation binding (first item should be the model name)
  • view (Wraith.View|Wraith.CollectionView) The view object to bind to.
  • twoWay (Boolean) Indicates if the view-model relationship should be two way. Typically used to bind form data to a model.

- (void) bindModelView(model, view)

Bind a model to a view and bind the view to the model (used for two-way model-view binding)

Parameters:

  • model (Wraith.Model) The model to bind
  • view (Wraith.View) The view to bind

- (void) unbindModelView(model, view)

Unbind a two-way model-view relationship.

Parameters:

  • model (Wraith.Model) The model to unbind
  • view (Wraith.View) The view to unbind

- (void) handleUIEvent(e, cb) (bound)

Handles the UI event which is mapped from the DOM to the controller.

Parameters:

  • e (Event) The event data object from the UI Event
  • cb (String) The name of the callback function to call on the controller

- (void) handleViewUIEvent(e, cb) (bound)

This is a wrapper for any UI event happening on views in this controller. We do this so we can do a lookup of the model and pass it through with the event

Parameters:

  • e (Event) The native event object
  • cb (String) The name of the callback function to call

- (Wraith.Model) getModelFromEl($el) (bound)

Traverses the dom upwards to find the first model it encounters If no model is found, it will return null

Parameters:

  • $el (HTMLElement) The element from which to start the traversal

Returns:

  • (Wraith.Model) — The model that belongs to the respective view

- (void) getInputDataFromEl($el)

Retrieve the input data from a given element

Parameters:

  • $el (HTMLElement) The element to iterate over and get form data from