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

circular dependency between action and webapi #57

Closed
jongbeau opened this issue Mar 8, 2015 · 7 comments
Closed

circular dependency between action and webapi #57

jongbeau opened this issue Mar 8, 2015 · 7 comments

Comments

@jongbeau
Copy link

jongbeau commented Mar 8, 2015

I was looking at the chat example and the pattern of an action calling the webapi which then calls another action after receiving the response from the API. If the web API wants to call an action from the same class as the original action, it results in a circular dependency with the require modules:

ActionClass -> requires WebAPI -> requires ActionClass

Any thoughts on how to structure this better?

@goatslacker
Copy link
Owner

If you're using commonjs you can use exports to get around this:

alt.createActions(ActionClass, exports)

@troutowicz
Copy link
Contributor

Another option is to use a callback.

class Actions {
  constructor() {
    this.generateActions('actionOne');
  }

  actionTwo() {
    WebAPIUtils.someAsyncOp((err, data) => {
      this.actions.actionOne(data);
    });
  }
}

@jongbeau
Copy link
Author

jongbeau commented Mar 8, 2015

@goatslacker - thanks, that worked! Can you explain what happens behind the scenes when using alt.createActions(ActionClass, exports) ?

@troutowicz , that was the approach I was going to take as last resort, but I like goatslacker's solution better.

@goatslacker
Copy link
Owner

The docs mention what that 2nd parameter is for: https://github.com/goatslacker/alt/blob/master/docs/createActions.md#createactions

Essentially it'll just add each action to exports. You can pass any object into createActions as its second parameter and it'll add all the actions to that object.

action.foo becomes exports.foo

Does that answer it or were you looking for a why does this work in commonjs when you use exports but not module.exports?

@jongbeau
Copy link
Author

jongbeau commented Mar 9, 2015

I'm more curious as to why it works, but doesn't when I use module.exports.

@jdlehman
Copy link
Collaborator

jdlehman commented Mar 9, 2015

@jongbeau Here is a relatively short article that should help explain what is happening under the covers.

Note that not all module systems do a great job at handling circular dependencies. I typically use webpack as it handles circular dependencies well and just like Node (in the way described in the article above).

@goatslacker
Copy link
Owner

Oh interesting didn't know that tidbit.

It works because the values get assigned to exports later on so they'll be available at run-time when you need them.

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

4 participants