fluo
Version:
A prototype-based fork of the Reflux data flow library similar to Facebook Flux
36 lines (31 loc) • 1.31 kB
JavaScript
var _ = require('./utils');
var Listener = require('./Listener');
/**
* A mixin factory for a React component. Meant as a more convenient way of using the `ListenerMixin`,
* without having to manually set listeners in the `componentDidMount` method.
*
* @param {Action|Store} listenable An Action or Store that should be
* listened to.
* @param {Function|String} callback The callback to register as event handler
* @param {Function|String} defaultCallback The callback to register as default handler
* @returns {Object} An object to be used as a mixin, which sets up the listener for the given listenable.
*/
module.exports = function(listenable,callback,initial){
return {
/**
* Set up the mixin before the initial rendering occurs. Import methods from `ListenerMethods`
* and then make the call to `listenTo` with the arguments provided to the factory function
*/
componentDidMount: function() {
this.__listener = new Listener();
_.link(this.__listener, this);
this.listenTo(listenable, callback, initial);
},
/**
* Cleans up all listener previously registered.
*/
componentWillUnmount: function () {
this.__listener.stopListeningToAll();
}
};
};