I'm using alt as my flux implementation for a project and am having trouble wrapping my head around the best way to handle loading stores for two related entities. I'm using sources feature along with registerAsync for to handle my async/api calls and bind them to my views using AltContainer.
I have two entities related one to one by the conversationId. Both are loaded via an api call:
Once my job store is loaded with data I want to fill a conversation store.
I use a source to load the job store:
module.exports = {
fetchJobs() {
return {
remote() {
return axios.get('api/platform/jobs');
},....
Seems like a job for the waitFor() method, but it seems for use when the contents of one store require a transformation or merging with the contents of another. I need to fetch the contents of one data store based on the contents of another.
In general terms I need to:
- Call a third party API and load a list of entities into a store.
- When that data arrives I need to use attribute from each of the above to call another API and load that data into another store.
My naive solution is to reference the conversation actions from the job store and to dispatch an event when the data arrives. Something like this:
var jobActions = require('../actions/Jobs');
var conversationActions = require('../actions/Conversations');
class JobStore {
constructor() {
this.bindListeners({
handlefullUpdate: actions.success
});...
}
handlefullUpdate(jobs) {
this.jobs = jobs;
conversationActions.fetch.defer(jobs);
}
}
Of course, doing this, violates the dictum that stores shouldn't dispatch events, and so I must use defer to dispatch an action in the middle of a dispatch. It makes sense to me, since it seems in going down this path I'm reintroducing all sorts of side effects in my code; losing the beauty of the "functional pipelines" that I should be seeing with flux.
Also, my job store has to hold a reference to any dependent entities so it can dispatch the appropriate action. Here I have only one, but I could imagine many. In terms of the dependencies between entities this seems totally backwards.
A couple of alternatives come to mind:
I can call the api/platform/jobs endpoint in the source/action where I fetch all conversations, just to get the id. The original approach is more efficient, but this seems truer to the spirit of flux in that I lose all the cross-talk.
I could also have single action/source that fetches both, returning {jobs:{}, conversations: in the action}
(orchestrating the dependency there using promises) and use this populate both stores. But this approach seems unnecessarily complicated to me (I feel like I shouldn't have to do it!).
But am I missing another way? It seems strange that such a common use case would break the elegance of the flux paradim and/or forces me to jump through so many hoops.
@dougajmcdonald posed a similar question here, but maybe it was phrased too generally, and didn't get any traction: