Skip to content
This repository was archived by the owner on Jan 11, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions app/scripts/actions/itemActions.js
Original file line number Diff line number Diff line change
@@ -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;
export default alt.createActions(ItemActions);
2 changes: 2 additions & 0 deletions app/scripts/alt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Alt from 'alt';
export default new Alt();
14 changes: 5 additions & 9 deletions app/scripts/components/itemList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -38,7 +34,7 @@ var ItemList = React.createClass({
</div>
);
}

});

module.exports = ItemList;
module.exports = ItemList;
44 changes: 19 additions & 25 deletions app/scripts/stores/itemStore.js
Original file line number Diff line number Diff line change
@@ -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;
export default alt.createStore(ItemStore, 'ItemStore');
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}