Mantra - Calling actions from other actions

I’m giving Mantra a test drive, and I’m liking what I see so far, but I’m not sure what the proper way to call actions from within other actions is. All the sample code I’ve seen so far has the actions just doing their own thing (i.e. comments actions only add comments and don’t touch anything else).

What if I want to chain actions together from different modules? For example (using https://github.com/mantrajs/meteor-mantra-kickstarter), when a user creates a new account, I want to also create a new post by that user. Should I just do all that within _users/actions/account.js? Or should I be calling the core/actions/posts.js action for creating a post?

If it’s using core/actions/posts.js how should I use that from within _users/actions/account.js?

We do not support calling actions from one action. If you need to do that, just create a library function and use it both place.

That’s clean and clear.

3 Likes

Where should this be placed?

core/libs/addPost.js (for example)

Then just import .../core/libs/addPost in both _users/actions/account.js and core/actions/posts.js?

1 Like

Yes. That’s correct.

1 Like

Just wondering what’s the reason for not supporting calling actions from another action?

We could enable that, but it’s kind a ugly with semantic wise. There no other reason than that.
But, sharing it via a library function is a much better and it shows re-usability.

So, that’s why we go with that approach.

2 Likes

Ok, that’s cool, but should I import both actions I want to chain in the library function? Wouldn’t that break modularity and make it harder to test, since the dependencies aren’t being injected?

Okay. You got a valid point on dependencies. This makes me to rethink on this topic again.

Passing the app actions into an action, alongside the app context, might be the way to achieve that, although it might not be the ideal one.

You can use this as well in needed: https://www.npmjs.com/package/mock-require
But, I’ll rethink about this.