Skip to content

Instantly share code, notes, and snippets.

@Nosherwan
Last active November 1, 2015 23:18
Show Gist options
  • Save Nosherwan/2308452185f7afb50c75 to your computer and use it in GitHub Desktop.
Save Nosherwan/2308452185f7afb50c75 to your computer and use it in GitHub Desktop.
Flux Article Store ES6 Syntax
import AppDispatcher from '../dispatcher/AppDispatcher';
import {EventEmitter} from 'events';
import AppConstants from '../constants/AppConstants';
let _articles = [];
function _addArticle(article) {
_articles.push(article);
}
function _removeArticle(id) {
delete _articles[id];
}
class ArticleStore extends EventEmitter {
constructor() {
super();
this.dispatchToken = AppDispatcher.register(this.dispatcherCallback.bind(this))
}
getAll() {
return _articles;
}
emitChange() {
this.emit(CHANGE_EVENT);
}
addChangeListener(callback) {
this.on(CHANGE_EVENT, callback);
}
removeChangeListener(callback) {
this.removeListener(CHANGE_EVENT, callback);
}
dispatcherCallback(action) {
switch (action.type) {
case ActionTypes.ARTICLE_CREATE:
_addArticle(action.article);
this.emitChange();
break;
case ActionTypes.ARTICLE_DELETE:
_removeArticle(action.articleId);
this.emitChange();
break;
}
return true;
}
}
export default new ArticleStore();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment