Une architecture pour le JavaScript front-end
ChtiJS #13 - 2/07/2015
</troll>
var ItemApp = (function () {
return React.createClass({
getInitialState: function() {
return { items: [] };
},
fetchItems: function() {
taLibAjaxFavorite
.get('/items')
.then(function (items) {
this.setState({ items: items });
});
},
createItem: function(data) {
taLibAjaxFavorite
.post('/items', data)
.then(function (items) {
this.setState({ items: items });
});
}
componentWillMount: function() {
this.fetchItems();
},
render: function() {
return (
<div>
<h1>Super useful item list</h1>
<ItemList items={this.state.items} />
<ItemForm createItem={this.createItem} />
</div>
);
}
});
})();
var ItemApp = (function () {
var getAppState = function() {
return { items: ItemStore.getItems() }; // Store
};
return React.createClass({
getInitialState: function() {
return getAppState();
},
onChange: function() {
this.setState(getAppState());
},
componentWillMount: function() {
ItemActions.fetchItems(); // Action
},
componentDidMount: function() {
ItemStore.addChangeListener(this.onChange); // Store
},
componentWillUnmount: function() {
ItemStore.removeChangeListener(this.onChange); // Store
},
render: function() {
return (
<div>
<h1>Super useful item list</h1>
<ItemList items={this.state.items} />
<ItemForm createItem={ItemActions.createItem /* Action */} />
</div>
);
}
});
})();
var ItemActions = (function () {
return {
fetchItems: function() {
Dispatcher.handleAction({
actionType: Constants.ITEM_FETCH
});
},
createItem: function(data) {
Dispatcher.handleAction({
actionType: Constants.ITEM_CREATE,
data: data
});
}
};
})();
var Dispatcher = (function () {
var dispatcher = new Flux.Dispatcher(); // dépendance 1
dispatcher.handleAction = function(action) {
this.dispatch({
action: action
});
};
return dispatcher;
})();
var ItemStore = (function () {
var items = [],
loadItems = function(data) {
items = data;
},
store = {};
Object.assign(
store,
EventEmitter.prototype, // dépendance 2
{
getItems: function() {
return items;
},
emitChange: function() {
this.emit('change');
},
addChangeListener: function(callback) {
this.addListener('change', callback);
},
removeChangeListener: function(callback) {
this.removeListener('change', callback);
}
}
);
// ...
// ...
Dispatcher.register(function (payload) {
switch (payload.action.actionType) {
case Constants.ITEM_FETCH:
taLibAjaxFavorite
.get('/items')
.then(function (data) {
loadItems(data);
store.emitChange();
});
break;
case Constants.ITEM_CREATE:
taLibAjaxFavorite
.post('/items', { value: payload.action.data })
.then(function (data) {
loadItems(data);
store.emitChange();
});
break;
}
return true;
});
return store;
})();