foxdonut/meiosis

Feature request: bind actions to Actions object

Closed this issue · 3 comments

Basically, adding this to the common.js before exporting everything;

  const bindActions = Object.entries(actions).reduce((acc, [key, func], index) => {
    acc[key] = func.bind(actions)
    return acc
  }, {})

  return { update, contexts, states, actions: bindActions }

This way we can call actions from inside actions with simple this.action. It would be a nice addition since you can include more logic between the actions inside them.
I have something similar, basically :

Actions : {
   api : { /* ... */ }
   paging: { /* ... */ }
   sorting: { /* ... */ }
}

where paging and sorting are internally calling the api action, and so on ...

@avionbg indeed this looks like it could be a nice addition, I will try it out. Thanks!

@avionbg I finally got a chance to try this out. Seems to be working without needing to make any changes. Please see this example. If you use function() { ... } instead of () => { ... }, an action can call another action with this.otherAction().

What do you think?

Further, you cannot bind this for arrow functions, as you can see here.