Last active
August 29, 2015 14:24
-
-
Save apisandipas/746e62bd598ccbacdfa4 to your computer and use it in GitHub Desktop.
'backflux'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Dispatcher = require('flux').Dispatcher; | |
| module.exports = new Dispatcher(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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