1. Home
  2. Docs
  3. Application Framework
  4. Javascript Framework
  5. Controllers

Controllers

mwp.controller Controller manager

A controller is used to build, maintain, and expose a new “view model” that can be used in html templates. It is also a convenient way to share functionality and state with other javascript modules, since a controller instance can be fetched from the mwp namespace.

@Reference

mwp.controller.model( name, properties, classProperties ) Define a controller model
mwp.controller.model.get( name) Get a controller model
mwp.controller.model.set( name, model ) Set/Override a controller model
mwp.controller.get( name ) Get a controller instance
mwp.controller.set( name, controller ) Set/Override a controller instance

@Controller Model

mwp.controller.model( name, properties, classProperties )

A controller model is what a controller instance is created from. The framework will wait until the page is fully loaded and then automatically instantiate each of the controllers which have been defined on a page. This allows other modules the opportunity to extend controller models after they have been defined, but before their singleton instance has been instantiated.

Internally, all controller models are created by extending the base Backbone.Model class, and therefore inherit all of the behaviors associated with a backbone model.

(function($, undefined){
    /**
     * Define a controller
     *
     * @param   string    name              The controller name
     * @param   object    properties        Methods and properties of model instances
     * @param   object    classProperties   Methods and properties of the model itself
     * @return  Backbone.Model
     */
    mwp.controller.model( 'example-controller', {
        init: function() {
            this.viewModel = {
                title: ko.observable( 'I am an example view model.' ),
            };
        }
    }); 
})(jQuery);

@Controller Instance

init() Initialization method

When the controller is instantiated, it’s init method is called. This is where any setup should be done to build the view model. The view model is a property of the controller instance named viewModel.

To get the instance of a controller, call mwp.controller.get( name ).

Was this article helpful to you? Yes No

How can we help?