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

Is it possible to get access to parent scopes on onEnter callback? #190

Closed
amitava82 opened this issue Jun 18, 2013 · 3 comments
Closed

Comments

@amitava82
Copy link

I'm looking at the dialog example, basically I want to define the dialog (with options and resolves etc) in parent scope and then in onEnter callback I'd just call:

dialog.open().then(function(result){
    $scope.$parent.doThing(result);
})

That way I can keep my state definition block clean.

@laurelnaiad
Copy link

If you inject $state to your controller then you can call $state.$current.parent . But doThing() is a strange thing to do to a parent... (happy belated Father's Day everyone!)

Seriously, though, depending on what's in the result that you want the opener of the dialog to handle, there are a few ways to approach the problem. Angular-ish ways include sharing a common controller that is further up the hierarchy and using a service. ui-router-specific ways tend to relate to state params.

Choosing wisely comes down to understanding what's in this result and what happens when your parent does his/her thing.

@amitava82
Copy link
Author

The idea I have is that each section of my app has a parent state and it holds all the common scope variables and method (such as doThing()) so that main CRUD logics are in one place. My child state would call for example, $scope.$parent.$save() if it needed. So in this example, I'm creating a dialog in a child state and dialog controller would calls method from parent state and pass callback result. I hope I made it clear.

Another doubt I have is that, I'm injecting all the dependencies in my parent state and assigning them to my scope variable so that I don't have to inject them into my child state as well. I can just use them from parent scope. Does it make good practice. I have not tested my controllers yet, but is it easy to test? Or I should stop being lazy and inject required services in other controllers as well?

@laurelnaiad
Copy link

I'd say "stop being lazy" (you said it first!) and post a little example of what you're trying to do that demonstrates your doubts! What isn't working? Nothing's easy to test if you don't try it!

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

2 participants