Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic parameters for actions executed by executeMultiple #24

Open
geekyme opened this issue Sep 28, 2015 · 2 comments
Open

dynamic parameters for actions executed by executeMultiple #24

geekyme opened this issue Sep 28, 2015 · 2 comments

Comments

@geekyme
Copy link

geekyme commented Sep 28, 2015

In the docs, I see this:


loadStuffForUser: [
            'loadUser',
            {
                // will be executed after 'loadUser' task completes successfully 
                action: require('../../actions/loadStuffForUser'),
                params: context.getStore(UserStore).getGUID()
            }
        ]

getGUID is ran at runtime when executeMultiple is called, so wouldn't the user not be loaded by the time the action "loadStuffForUser" is called?

@geekyme
Copy link
Author

geekyme commented Sep 28, 2015

for instance right now i have a piece of application code like this:

actionUtils.executeMultiple(context, {
      user: {
        action: getUser
      },
      location: {
        action: getLocation
      },
      groups: [
        "location",
        {
          action: getAll,
          params: {
            countryCode: context.getStore("ApplicationStore").getLocation().get("country_code")
          }
        }
      ],

....

I realized that the getAll action creator is getting called with countryCode = undefined because ApplicationStore is still not hydrated with the location data that is provided only after the action creator getLocation has been run.

As a result I will have to place the code fragment context.getStore("ApplicationStore").getLocation().get("country_code") into the getAll action creator to make this work, however, this makes the action creator difficult to test because it is now getting data from else where other than its arguments.

@geekyme
Copy link
Author

geekyme commented Sep 29, 2015

@koulmomo

At the moment I attempt to solve this by wrapping around an action:

export default function provideActionParams(c, func, provider) {
  return function(context, params, done){
    const otherParams = provider(c);
    return func(context, { ...params, ...otherParams }, done);
  };
}

This can be used together with action.executeMultiple like this:

actionUtils.executeMultiple(context, {
      groups: [
        {
          action: provideActionParams(context, getGroups, function(context){
            return {
              countryCode: context.getStore("ApplicationStore").getLocation().get("country_code")
            };
          })
        }
      ]
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant