From 5e860dd97ebac76e35deaaa7e13a9a17c5c3443a Mon Sep 17 00:00:00 2001 From: Josh Perez Date: Thu, 26 Feb 2015 22:07:19 -0800 Subject: [PATCH] Replace reflux with alt --- app/scripts/actions/itemActions.js | 37 +++++++++++++----------- app/scripts/alt.js | 2 ++ app/scripts/components/itemList.jsx | 14 ++++----- app/scripts/stores/itemStore.js | 44 +++++++++++++---------------- package.json | 4 +-- 5 files changed, 48 insertions(+), 53 deletions(-) create mode 100644 app/scripts/alt.js diff --git a/app/scripts/actions/itemActions.js b/app/scripts/actions/itemActions.js index 6e38b6d..13373cd 100644 --- a/app/scripts/actions/itemActions.js +++ b/app/scripts/actions/itemActions.js @@ -1,21 +1,24 @@ -import Reflux from 'reflux'; +import alt from '../alt'; -var ItemActions = Reflux.createActions([ - 'loadItems', - 'loadItemsSuccess', - 'loadItemsError' -]); +class ItemActions { + constructor() { + this.generateActions( + 'loadItemsSuccess', + 'loadItemsError' + ); + } -ItemActions.loadItems.preEmit = function(data){ - // make your api call/ async stuff here - // we use setTimeout for faking async behaviour here - setTimeout(function(){ - var items = ['Foo', 'Bar', 'Lorem']; - ItemActions.loadItemsSuccess(items); + loadItems(data) { + // make your api call/ async stuff here + // we use setTimeout for faking async behaviour here + setTimeout(() => { + var items = ['Foo', 'Bar', 'Lorem']; + this.actions.loadItemsSuccess(items); - // on error - // ItemActions.loadItemsError('an error occured'); - },500); -}; + // on error + // ItemActions.loadItemsError('an error occured'); + }, 500); + } +} -module.exports = ItemActions; \ No newline at end of file +export default alt.createActions(ItemActions); diff --git a/app/scripts/alt.js b/app/scripts/alt.js new file mode 100644 index 0000000..bd2bdab --- /dev/null +++ b/app/scripts/alt.js @@ -0,0 +1,2 @@ +import Alt from 'alt'; +export default new Alt(); diff --git a/app/scripts/components/itemList.jsx b/app/scripts/components/itemList.jsx index 5433f90..bf340fa 100644 --- a/app/scripts/components/itemList.jsx +++ b/app/scripts/components/itemList.jsx @@ -5,20 +5,16 @@ import ItemActions from '../actions/itemActions'; var ItemList = React.createClass({ getInitialState() { - return { - items : [], - loading : false, - error : false - } + return ItemStore.getState(); }, componentDidMount() { - this.unsubscribe = ItemStore.listen(this.onStatusChange); + ItemStore.listen(this.onStatusChange); ItemActions.loadItems(); }, componentWillUnmount() { - this.unsubscribe(); + ItemStore.unlisten(this.onStatusChange); }, onStatusChange(state) { @@ -38,7 +34,7 @@ var ItemList = React.createClass({ ); } - + }); -module.exports = ItemList; \ No newline at end of file +module.exports = ItemList; diff --git a/app/scripts/stores/itemStore.js b/app/scripts/stores/itemStore.js index 9b99c87..1f199fe 100644 --- a/app/scripts/stores/itemStore.js +++ b/app/scripts/stores/itemStore.js @@ -1,38 +1,32 @@ -import Reflux from 'reflux'; +import alt from '../alt'; import ItemActions from '../actions/itemActions'; -var ItemStore = Reflux.createStore({ - - init() { +class ItemStore { + constructor() { this.items = []; + this.loading = false; + this.error = null; - this.listenTo(ItemActions.loadItems, this.loadItems); - this.listenTo(ItemActions.loadItemsSuccess, this.loadItemsSuccess); - this.listenTo(ItemActions.loadItemsError, this.loadItemsError); - }, + this.bindListeners({ + loadItems: ItemActions.loadItems, + loadItemsSuccess: ItemActions.loadItemsSuccess, + loadItemsError: ItemActions.loadItemsError + }); + } loadItems() { - this.trigger({ - loading: true - }); - }, + this.loading = true; + } loadItemsSuccess(items) { this.items = items; - - this.trigger({ - items : this.items, - loading: false - }); - }, + this.loading = false; + } loadItemsError(error) { - this.trigger({ - error : error, - loading: false - }); + this.error = error; + this.loading = false; } +} -}); - -module.exports = ItemStore; \ No newline at end of file +export default alt.createStore(ItemStore, 'ItemStore'); diff --git a/package.json b/package.json index 55b45b6..1a3eb92 100644 --- a/package.json +++ b/package.json @@ -35,8 +35,8 @@ "webpack": "^1.5.3" }, "dependencies": { + "alt": "^0.13.10", "react": "^0.12.2", - "react-router": "^0.12.0", - "reflux": "^0.2.5" + "react-router": "^0.12.0" } }