Model
What is this ?
The Model is the class that will be responsible for storing your contextualData and pass them to your ModelHelper. It doesn't do much more but it do it well. The Model is a singleton object. So when you want to use it, you will use Model.getInstance(). You should NOT call the Model from a ControllerHelper, only from ModelHelpers.
How to use the Model for communication between ModelHelper ?
You know you modules needs to communicate between each other even if they don't really know each other. So how let them communicate ? The answer is the setContextualData() method.
Here is an example with two modelHelper that want to communicate :
In model helper 1 :
var Module1Model = Class.create(ModelHelper, {
...
init: function($super) {
$super();
},
contextualDataChangeHandler: function($super, name, value) {
switch(name) {
case 'titi';
alert(value);
break;
default:
break;
}
},
...
});
In model helper 2 :
var Module2Model = Class.create(ModelHelper, {
...
init: function($super) {
$super();
Model.getInstance().setContextualData('titi', 42);
},
...
});
As you can see the module 2 want to say to module 1 that titi is 42. So it use the Model.getInstance().setContextualData() method. Then the contextualDataChangeHandler() of the Module1Model will be called. The switch case will case 'titi' and alert 42. Cool, isn't it ?
In fact, the contextualDataChangeHandler() of Module2Model is also called (and all other contextualDataChangeHandler() method from all your other modelHelper of your application). To avoid that, you can specify a third parameter to Model.getInstance().setContextualData(). This could be either a string or an array and will specify which model helper should receive the data.
Let say you have 3 model helper and you want that model helper 1 communicate a data to model helper 2 and only model helper 2 :
In model helper 1 :
var Module1Model = Class.create(ModelHelper, {
...
init: function($super) {
$super();
Model.getInstance().setContextualData('titi', 42, ['Module2Model']);
// or Model.getInstance().setContextualData('titi', 42, 'Module2Model');
},
contextualDataChangeHandler: function($super, name, value) {
switch(name) {
case 'titi';
alert(value); // will not alert
break;
default:
break;
}
},
...
});
In model helper 2 :
var Module2Model = Class.create(ModelHelper, {
...
contextualDataChangeHandler: function($super, name, value) {
switch(name) {
case 'titi';
alert(value); // will alert 42
break;
default:
break;
}
},
...
});
In model helper 3 :
var Module2Model = Class.create(ModelHelper, {
...
contextualDataChangeHandler: function($super, name, value) {
switch(name) {
case 'titi';
alert(value); // will not alert
break;
default:
break;
}
},
...
});
If you have a lot of module in your application, it can really speed up your communication if you specify the third argument.
Model methods
setContextualData(key, value, destinations)
As we just see, this method will help you to let your modelHelper communicate between each other.
Parameter key is a string.
Parameter value can be whatever you want : an object, a string, an int...
Parameter destinations can be a string (only one destination) or an array (more than one destination). Destination is the name of the model helper you wish to communicate with.
Model.getInstance().setContextualData('toto');
Model.getInstance().setContextualData('titi', 42, 'Module2Model');
Model.getInstance().setContextualData('titi', {b: 42, c: [1, 3]}, ['Module2Model', 'Module3Model']);
getContextualData(key)
Will return the contextual data corresponding to the asked key.
Model.getInstance().setContextualData('titi', 42);
Model.getInstance().getContextualData('titi'); // --> 42