In refluxjs we solve waitFor
in a couple of ways, one for the sequential data flow and the other for the parallel data flow. I try to model the data stores in a way to avoid holding the same data (i.e. double maintenance data).
Basically, data stores are CQRS components, and I try to avoid having 2 data stores end up with the same kind of data. If I need to transform the data somehow that only some components need, I break that out to an "aggregate" data store. Naïve implementation:
var carsStore = Reflux.createStore({
init: function() {
this.listenTo(Actions.updateCars, this.updateCallback);
},
updateCallback: function() {
$.ajax('/api/cars', {}).done(function(data) {
this.trigger(data.cars);
}.bind(this));
}
});
We can create another data store that aggregates the data by listening to the carsStore
:
var modelsStore = Reflux.createStore({
init: function() {
this.listenTo(carsStore, this.carsCallback);
},
carsCallback: function(cars) { // passed on from carsStore trigger
this.trigger(this.getModels(cars)); // pass on the models
}
getModels: function(cars) {
return _.unique(_.map(cars, function(car) { return car.model; }));
}
});
That way your React view components may use one to get the cars and the other to get the models, which is aggregated from the carStore
.
If a store needs to wait for two parallell data streams to complete we provide the Reflux.all
to join actions and stores. This is useful e.g. if you're waiting for data to load from seperate different REST resources.
var carsAndPartsAreLoaded = Reflux.all(carStore, partsStore);
// you may now listen to carsAndPartsAreLoaded
// from your data stores and components
Hope this makes sense to you.