Skip to content

Instantly share code, notes, and snippets.

@kitten
Last active August 29, 2015 14:21
Show Gist options
  • Save kitten/31140aed46fc6950646b to your computer and use it in GitHub Desktop.
Save kitten/31140aed46fc6950646b to your computer and use it in GitHub Desktop.
React ES6 Code Samples for Medium #4
var AppDispatcher = require('../dispatcher/AppDispatcher');
var EventEmitter = require('events').EventEmitter;
var assign = require('object-assign');
var CHANGE_EVENT = 'change';
var _elements = {};
function create(obj) {
_elements[obj.id] = obj;
ExampleStore.emitChange();
}
function destroy(id) {
delete _elements[id];
ExampleStore.emitChange();
}
var ExampleStore = assign({}, EventEmitter.prototype, {
getAll: function() {
return _elements;
},
getList: function() {
var res = [];
for (var key in _elements) {
res.push(_elements[key]);
}
return res;
}
emitChange: function() {
this.emit(CHANGE_EVENT);
},
addChangeListener: function(callback) {
this.on(CHANGE_EVENT, callback);
},
removeChangeListener: function(callback) {
this.removeListener(CHANGE_EVENT, callback);
}
});
// ... Dispatcher Code ...
module.exports = ExampleStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment