-
Notifications
You must be signed in to change notification settings - Fork 324
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
Comments
If you're using commonjs you can use
|
Another option is to use a callback. class Actions {
constructor() {
this.generateActions('actionOne');
}
actionTwo() {
WebAPIUtils.someAsyncOp((err, data) => {
this.actions.actionOne(data);
});
}
} |
@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. |
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? |
I'm more curious as to why it works, but doesn't when I use module.exports. |
@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). |
Oh interesting didn't know that tidbit. It works because the values get assigned to |
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?
The text was updated successfully, but these errors were encountered: