Controller Helper
What's in it ?
For each module, you have at least a model helper and a controller helper. Here is the Controller Helper.
This class will be the one that will manage the event handlers. It also knows the view (it is a controller). Its main job will be to call his model helper.
Let's take a look
Here is an example of a small Controller Helper :
var ModuleController = Class.create(ControllerHelper, {
initialize: function($super) {
$super();
},
init: function($super) {
$super();
},
inputClickHandler : function(event) {
var element = event.element();
var value = element.getValue();
this.launchModelMethod('manageData', value);
}
});
As you can see, this class extends from the base class ControllerHelper. It has three methods initialize (the constructor), init (the method launch by the framework once the module is done initializing) and inputClickHandler (a method defined by you in the module configuration file).
Playing with listeners in the controller helper
Let's say you have in your module configuration file :
... <bindAsListener id="button" event="click" handler="buttonClickHandler"/> ...
In your HTML file :
... <button id="button">Click Me !</button> ...
And in your ControllerHelper
var ModuleController = Class.create(ControllerHelper, {
...
buttonClickHandler: function(event) {
var element = event.element();
alert(element.id); // button
alert(element.innerHTML); // Click Me !
},
...
});
When the user will click on the button, the method buttonClickHandler will be launch and alert button and then Click Me !
Passing data from the controllerHelper to the modelHelper
As we saw before, one of the job of the controllerHelper is to pass the data from the view so your model can manage them and calculate stuff for example. To do that, we are using the launchModelMethod method. Let's show you :
In your ControllerHelper :
var ModuleController = Class.create(ControllerHelper, {
...
buttonClickHandler: function(event) {
var element = event.element();
var value = element.innerHTML;
var res = this.launchModelMethod('calculateData', value);
alert(res); // --> 3
},
...
});
In your ModelHelper :
var ModuleModel = Class.create(ModelHelper, {
...
calculateData: function(value) {
var sp = value.split(' '); // ['Click', 'Me', '!']
this.len = sp.length; // 3 (save the data in the propertie len for a later use)
return this.len;
},
...
});
ControllerHelper methods
initialize()
This is the constructor of your controllerHelper. Nothing special here, just call the $super() method in it.
var ModuleController = Class.create(ControllerHelper, {
initialize: function($super) {
$super();
},
...
});
init()
This method is the one which is automaticaly called by the framework once your module is done loading. Call $super() in it.
var ModuleController = Class.create(ControllerHelper, {
init: function($super) {
$super();
},
...
});
launchModelMethod(modelMethodName, ...args)
As we just saw, this is the method you use to communicate with the modelHelper. First argument is the name of the model method. You can add as many parameters after the model method name.
It will return the value return by the method called.
In opposition of the launchControllerMethod, the called method will be launch directly.