Skip to content

Instantly share code, notes, and snippets.

@colinf
Last active July 13, 2016 16:09
Show Gist options
  • Save colinf/b15f8fb9bb32d3cf474a to your computer and use it in GitHub Desktop.
Save colinf/b15f8fb9bb32d3cf474a to your computer and use it in GitHub Desktop.
Standard Flux Store definition in ES5 ( See http://bit.ly/29RY6gq )
var AppDispatcher = require('../dispatcher/AppDispatcher');
var AppConstants = require('../constants/AppConstants');
var EventEmitter = require('events').EventEmitter;
var assign = require('object-assign');
var ActionTypes = AppConstants.ActionTypes;
var CHANGE_EVENT = 'change';
var ExampleStore = assign({}, EventEmitter.prototype, {
emitChange: function() {
this.emit(CHANGE_EVENT);
},
addChangeListener: function(callback) {
this.on(CHANGE_EVENT, callback);
},
removeChangeListener: function(callback) {
this.removeListener(CHANGE_EVENT, callback);
},
// define other Store methods to query data etc
});
ExampleStore.dispatchToken = AppDispatcher.register(function(action) {
switch(action.type) {
case ActionTypes.CLICKED_IT:
// update the store based on this action;
this.emitChange();
break;
// case statements for other actions relevant to this store
default:
// do nothing
}
});
module.exports = ExampleStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment