Skip to content

Instantly share code, notes, and snippets.

@apisandipas
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save apisandipas/746e62bd598ccbacdfa4 to your computer and use it in GitHub Desktop.

Select an option

Save apisandipas/746e62bd598ccbacdfa4 to your computer and use it in GitHub Desktop.
'backflux'
var Backbone = require('backbone');
var Dispatcher = require('./dispatcher');
var BaseStore = {
/**
* backbone init method
* - the Dispatcher registers this stores handleDispatch method and return a reference to self, the dispatchId.
*/
initialize: function() {
this.dispatchId = Dispatcher.register(this.handleDispatch.bind(this));
},
/**
* handle the dispatcher actions
* @param {Object} payload
*/
handleDispatch: function(payload) {}
};
module.exports = {
Model: Backbone.Model.extend(BaseStore),
Collection: Backbone.Collection.extend(BaseStore)
};
var Dispatcher = require('flux').Dispatcher;
module.exports = new Dispatcher();
/**
* mixin to let components listen to stores in a simple way
* the component needs to implement `onStoreUpdate` to set the state
* @param {Object} store
* @param {String} [events="add remove reset change"]
*/
module.exports = function(store, events) {
if (!events) {
events = "add remove reset change";
}
return {
componentDidMount: function() {
store.on(events, function() {
this.forceUpdate();
}, this);
},
componentWillUnmount: function() {
store.off(null, null, this);
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment