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

Support nested actions #5

Open
reohjs opened this issue Apr 16, 2016 · 0 comments
Open

Support nested actions #5

reohjs opened this issue Apr 16, 2016 · 0 comments

Comments

@reohjs
Copy link

reohjs commented Apr 16, 2016

Hi,

I have a large app and find it beneficial to sometimes categorise the actions in my Mantra modules:

posts.list.created
posts.list.deleted
posts.admin.edit
// etc...

Possible implementation:

export function injectDeps(context, _actions) {
  const actions = {};

  function bindNested (obj) {
      var memo = {};
      for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
          if (typeof obj[key] === 'object') {
            memo[key] = bindNested(obj[key]);
          } else {
            memo[key] = obj[key].bind(null, context);
          }
        }
      }
      return memo;
    }

    for (var key in _actions) {
      if (_actions.hasOwnProperty(key)) {
        var actionMap = _actions[key];
        var newActionMap = {};
        for (var actionName in actionMap) {
          if (actionMap.hasOwnProperty(actionName)) {
            var actionOrObj = actionMap[actionName];
            if (typeof actionOrObj === 'object') {
              newActionMap[actionName] = bindNested(actionOrObj);
            } else {
              newActionMap[actionName] = actionOrObj.bind(null, context);
            }
          }
        }
        actions[key] = newActionMap;
      }
    }
  // etc...

Let me know if you'd accept a PR for this :)

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