Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Latest commit

 

History

History
62 lines (44 loc) · 2.02 KB

module-helpers.md

File metadata and controls

62 lines (44 loc) · 2.02 KB

Helpers

Helpers are small reusable plug-ins that you can write to add extra features to a View module (working example).

Defining helpers

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;
  });
};
  1. It is often useful to hook into the before setup event so that added functionality is available inside the module's setup function.

Attaching helpers

At definition:

var Apple = fruitmachine.define({
  name: 'apple',
  helpers: [ myHelper ]
});

...or instantiation:

var apple = new Apple({
  helpers: [ myHelper ]
});

Using features

apple.sayName();
//=> 'My name is apple'

Community Helpers ("Plugins")

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.