Flux as a design pattern is built on top of the idea that all data resides in "stores". Each store holds data for a given domain of information. As an example: in Flux, all comments would reside in a CommentStore.
When data is changed in a store, it should emit an event and all components that builds on top of this "information domain", should rerender and display the new domain data.
I've found that when a store is emitting multiple event types, it is more likely that components are not listening for that specific event, thus not rerendering itself when the domains data is altered.
This breaks the whole flux-pattern, and can easily create hard to find bugs where components are out of sync with the stores information.
I would instead recommend that you design your components from the "Law of Demeter" - each component should only know as much as it needs to.
So instead of the component listening to an event that says "commentList has been updated", you should create a commentListComponent that listens on a single store event. Thus, the component will listen on commentStore.on('change') - I usually let all stores emit an 'change' event. When the store emitts, you should rerender the data in the commenListComponent to reflect the store. If you use React, this is where you use setState.
var commentStore = _.extend({}, EventEmitter.prototype, {
updateComents: function() {
// Update comments and emit
this.emit('change');
},
removeComments: function() {
// Remove comments and emit
this.emit('change');
},
getState: function() {
return {
comments: this.comments,
someOtherDomainData: this.meta,
}
}
});
//commentListComponent.js
var commentListComponent = React.createClass({
componentDidMount : function() {
commentStore.on('change', this._commentChanged);
},
componentWillUnmount : function() {
commentStore.off('change', this._commentChanged);
},
_commentChanged : function() {
this.setState({ comments : commentStore.getState().comments });
},
render : function() {
var comments = // Build a list of comments.
return <div>{comments}</div>
}
})
This makes the data flow much more simple, and avoids hard to spot errors.
react
specifically, but the main considerations in general are balancing writing a lot of boilerplate wire-up code to have discrete event types for every entity vs having every update event handler fire whenever anupdate
is published instead of having one event handler fire when auserUpdated
is published. How much horsepower does your runtime environment have? – Grijalva