Controller
What is this ?
The Controller is the class that is mainly responsible of managing your modules (dynamic load...). It is also reponsible of applying method to an element in the DOM. This is a singleton class. That means you have to call it using Controller.getInstance(). You should NOT call the Controller in a model helper, only from controller helpers.
How to dynamically load a module using the controller ?
If your remember what we saw in the MVC configuration file section, you know that some module can be loaded dynamically by putting the executeAtInit to false for the module item. Let's see a full example :
In the mvc.xml file :
<?xml version="1.0" encoding="utf-8"?>
<files>
<item id="Module1"
file="application/module1/conf.xml"
executeAtInit="true" // loaded at the initialization
/>
<item id="Module2"
file="application/module4/conf.xml"
executeAtInit="false" // loaded dynamically
/>
</files>
In Module1Controller :
var Module1Controller = Class.create(ControllerHelper, {
...
init: function($super) {
$super();
},
myFunction: function() {
Controller.getInstance().loadModule('Module2', {titi: 42});
},
...
});
In Module2Model :
var Module1Model = Class.create(ModelHelper, {
...
init: function($super, obj) {
$super();
alert(obj.titi); // --> 42
},
...
});
As you can see, the Module2 is not loaded during the initialization. Once you launch the myFunction method in Module1Controller, we ask to the Controller to load the module Module2. The Controller will load the Module2 configuration file, the Module2Controller, the Module2Model and the plugins used by the module. Once everything loaded and initialized, both init method of Module2Model and Module2Controller are called. Here, when we called the loadModule method, we passed a second argument {titi: 42}. This is the object you will find in the init method of Module2Model.
If you wish later to unload the module Module2, you will have to call :
Controller.getInstance().unLoadModule('Module2');
Controller methods
loadModule(id, initObj, executeBindings)
As we just see, the loadModule method will help you to load a module. If a module is loaded and you call this method on it, it will not load it again. So don't forget to call unLoadModule once you are done using a module.
First argument is the id of the module you which to load. The id is the one you defined in your mvc.xml file.
initObj is an object or anything else you which to pass to the module you are loading. You will find it in the init method of the ModelHelper of the module you are loading.
executeBindings is a boolean. If you set it to false, the framework will not execute the binding found in the module configuration file. Default to true.
unLoadModule(id, willReload)
unLoadModule will unload a module previously loaded.
First argument is the id of the module you which to unload. The id is the one you defined in your mvc.xml file.
willReload is a boolean. If you set it to false, the framework will unload the module configuration file, the controller helper and the model helper of your module. If you set it to true, the framework will save some information, so next time you will load the module, it will be really fast. Default to true.
applyMethodToElement(idOfTheElement, methodToCall, ...argsForTheMethodToCall)
This is a method you call from controller helpers to apply a method to an element in the DOM
var ModuleController = Class.create(ControllerHelper, {
...
init: function($super) {
$super();
Controller.getInstance().applyMethodToElement('textInput', 'focus');
},
...
});
idOfTheElement is the id of the element in the DOM you which to apply the method on.
methodToCall is the name of the method.
The rest of the arguments will be passed to the method you are calling.
refreshModule(id)
If you are using a multiple module, you could dynamically add some items in your list. This method will help you to add the new items in your list of module multiple.
getCurrentMultitonNumber(id)
This method will return the number of module multiple already in place for the module with the id id.
getNextMultitonId(id)
Same as getCurrentMultitonNumber plus one.