Last active
August 29, 2015 14:21
-
-
Save kitten/31140aed46fc6950646b to your computer and use it in GitHub Desktop.
React ES6 Code Samples for Medium #4
This file contains 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 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