Async requests with data from store

129 views Asked by At

I just started using refluxjs/react with webpack. I really liked the listenAndPromise async pattern, but I've encountered a problem. I got the following code:

Action:

var ResumeService = require('services/ResumeService');
var ResumeActions = Reflux.createActions({
    'save': {
        asyncResult: true
    }
});
ResumeActions.save.listenAndPromise(ResumeService.save);

ResumeService:

var http = require('utils/http');
var ENDPOINT = '/resumes/';
module.exports = {
    save: function() {
        return http.post(ENDPOINT + 'save', {
            resume: ??????????
        });
    },
}

And part of the store:

var RendererActions = require('actions/RendererActions');
var ResumeStore = Reflux.createStore({
    listenables: [ResumeActions],
    resume: Immutable.fromJS(ResumeExample),  //nested object

    onSaveCompleted: function(id) {
        .....
    },

    onSaveFailed: function() {
        ....
    }
    ...
});

So when a user makes change, the resume variable in the store is updated. Now the user wants to save the changes and he hit a button which triggers Action.save(). Here comes my problem: How can I pass the resume variable from the store to the service file above?

The weird thing is that if I do var Store = require('stores/ResumeStore'); in the service file, it has the value object {}, this happens only if I include the store in the service, I think it has something to do with endless loop requiring or something like this. ( Store requires Action, Action requires Service, Service requires Store )

1

There are 1 answers

0
Hannes Johansson On

Why don't you just add the resume as a parameter in the save method in ResumeService?

module.exports = {
    save: function(resume) {
        return http.post(ENDPOINT + 'save', {
            resume: resume
        });
    },
}

Then ResumeActions.save.listenAndPromise(ResumeService.save); should work. listenAndPromise just passes the action payload through to its callback argument.