Helpers are small reusable plug-ins that you can write to add extra features to a View module (working example).
A helper is simply a function accepting the View module instance as the first argument. The helper can listen to events on the View module and bolt functionality onto the view.
Helpers should clear up after themselves. For example if they create variables or bind to events on setup
, they should be unset and unbound on teardown
.
var myHelper = function(module) {
// Add functionality
module.on('before setup', function() { /* 1 */
module.sayName = function() {
return 'My name is ' + module.name;
};
});
// Tidy up
module.on('teardown', function() {
delete module.sayName;
});
};
- It is often useful to hook into the
before setup
event so that added functionality is available inside the module'ssetup
function.
At definition:
var Apple = fruitmachine.define({
name: 'apple',
helpers: [ myHelper ]
});
...or instantiation:
var apple = new Apple({
helpers: [ myHelper ]
});
apple.sayName();
//=> 'My name is apple'
Helpers can be released as plugins, if you would like to submit your helper to this list please raise an issue.
- fruitmachine-ftdomdelegate provides ftdomdelegate functionality within fruitmachine modules.
- fruitmachine-bindall automatically binds all the methods in a module to instances of that module.
- fruitmachine-media allows you to create responsive components. Set up media queries for different states and this plugin will allow you to hook into per state setup and teardown events when those media queries match.