Let's analyze how your default Program Controller works.
To begin with, open up your default Program Controller in [project]/assets/opstools/[ToolName]/controllers/[ToolName].js.
It should look like this (without comments) :
steal(
// List your Controller's dependencies here:
'appdev',
'//OpsPortal/classes/OpsTool.js',
'/opstools/[ToolName]/views/[ToolName]/[ToolName].ejs',
function(){
AD.Control.OpsTool.extend('[ToolName]', {
init: function (element, options) {
var self = this;
options = AD.defaults({
templateDOM: '//opstools/[ToolName]/views/[ToolName]/[ToolName].ejs',
resize_notification: '[ToolName].resize'
}, options);
this.options = options;
// Call parent init
this._super(element, options);
this.initDOM();
},
initDOM: function () {
this.element.html(can.view(this.options.templateDOM, {} ));
}
});
});The first part of the file specifies the dependencies required before this code can be run. In this case we are using JavascriptMVC's steal library to specify our dependencies.
steal(
// List your Controller's dependencies here:
'appdev',
'//OpsPortal/classes/OpsTool.js',
'/opstools/[ToolName]/views/[ToolName]/[ToolName].ejs',
function(){Currently this controller depends on our
- appdev library
- the OpsTool.js class defined in our OpsPortal
- and the default view [ToolName].ejs
Note: all the paths are specified from the
[sails]/assetsdirectory.
StealJS will load all those packages before running the fn() that contains our Controller.
Our appdev library provides an API for creating new OpsTools:
AD.Control.OpsTool.extend( [ControllerName], { Controller Definition } );in our { Controller Definition } we defined two methods:
init(): called by CanJS objects when they are createdinitDom(): called at the end of init() to setup our initial view.
our initDom() actually pushes our HTML view to the DOM:
this.element.html():thiscontroller is attached to a DOMelement, and we are setting thehtml()of that element to the data we send the function: which is fromcan.view( 'templateURL', {data})