Last active
November 1, 2015 23:18
-
-
Save Nosherwan/2308452185f7afb50c75 to your computer and use it in GitHub Desktop.
Flux Article Store ES6 Syntax
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
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