Flux/Alt data dependency, how to handle elegantly and idiomatically

后端 未结 3 1718
猫巷女王i
猫巷女王i 2021-01-17 18:55

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 usi

3条回答
  •  攒了一身酷
    2021-01-17 19:45

    The best solution I've come with with so far (and the one that the examples seem to follow) is to add a "jobsFetched" action to my jobs actions and to dispatch it when the data arrives.

    var jobActions = require('../actions/Jobs');
    var ConversationActions = require('../actions/Conversations');
    
    class JobStore {
        constructor() {
            this.bindListeners({
                handlefullUpdate: jobActions.success...
            });...
    
        }
    
        handlefullUpdate(jobs) {
            this.jobs = jobs;
            ConversationActions.jobsFetched.defer(jobs);
        }
    }
    
    
    class ConversationStore {
        constructor() {
            this.bindListeners({
                handleJobUpdate: jobActions.jobsFetched...
            });...
    
        }
    
        handleJobUpdate(jobs) {
    
            /*Now kick off some other action like "fetchConversations" 
              or do the ajax call right here?*/
    
        }
    
    }
    

    This eliminates the problem of the jobs store having to hold a reference to all its dependent objects, But I still have to call an action from inside my store, and I have to introduce jobsFetched action which sets up the ConversationStore to fetch it's data. So it seems like I can't use a source for my conversations.

    Can anyone do better?

提交回复
热议问题