Skip to content

resolve is always called with notify:false #2146

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

Closed
davideicardi opened this issue Jul 31, 2015 · 15 comments
Closed

resolve is always called with notify:false #2146

davideicardi opened this issue Jul 31, 2015 · 15 comments
Labels

Comments

@davideicardi
Copy link

I want to change the state without reloading the controller. All seems to work fine except that my resolve function is always called.

To change state I use:

$state.go("home", {param1:"changed" + changes++}, { notify : false});

Here my plnkr: http://plnkr.co/edit/jo3C56ZwkPE27FtRU1Y6

Basically I log each time the controller is loaded or the resolve is called, then I added a button to change the state. As you can see resolve is always called also with notify = false.

What do you think? My problem is that inside the resolve I load some resource and I want this code to be called only when really changing state, not when changing state using notify=false.

I use version ui-router 0.2.15 and angular 1.4.3.

@davideicardi davideicardi changed the title Calling $state.go with notify = false always call resolve function When calling $state.go with notify = false resolve function is executed Jul 31, 2015
@eddiemonge eddiemonge added the bug label Jul 31, 2015
@eddiemonge
Copy link
Contributor

Does seem like a bug

@davideicardi
Copy link
Author

Thinking more about this...notify is documented as:

If true will broadcast $stateChangeStart and $stateChangeSuccess events

Documentation doesn't mention nothing about controller and resolve feature.
My goal is to change the state (logically and also changing the location) without reloading the controller and without calling the resolve method again. Is the notify:false the right approach?

thanks

@davideicardi
Copy link
Author

Looking at the code I have a possible solution, here my fork+commit: https://github.com/davideicardi/ui-router/commit/7198aa2b3618f11b376949a80e2b7c4c6640cd4c

Basically I have checked the notify options inside shouldSkipReload function. I have also added a unit test. (don't know why there are also other CRLF changes...).
If you want I can create a pull request.

Eventually I have also a different workaround not involving changing any ui-router code: plunker. I have added a custom "resolve" params and use it as an option to check if calling or not the resolve functions. So I can call the $state.go like this:

$state.go("home", {param1:"changed" + changes++, resolve: false}, { notify : false});

But I don't like this solution, seems to be just a workaround.

What do you think? I'm not sure what is the right approach...

@PhiLhoSoft
Copy link

Seems related to #2108 and #2087

@eddiemonge
Copy link
Contributor

#2087 has the controller reinitialize when changing to a different state. This has the resolve fired but not the controller so its slightly different.

@nateabele
Copy link
Contributor

Documentation doesn't mention nothing about controller and resolve feature.

Right... that's because it's totally unrelated: notify is only about events. Why is this confusing?

@davideicardi
Copy link
Author

My final goal is to change the state and browser URL (actually I just need to change a state par) without reloading the controller or calling the resolve. What is the best solution for you? I just don't understand how to solve my problem.
Thanks!

@eddiemonge
Copy link
Contributor

Why?

@davideicardi
Copy link
Author

Basically I have backoffice application where the user can add new articles/stories. So I have a state like "/stories/{id}".
When the user want to create a story I go to "/stories/new". He fills some fields and then using an autosave feature (more or less like Google doc) the story is saved and I assign it an Id. So I want to change the state and go to "/stories/57543", but I don't actually need to reload anything.
Maybe there is another solution?

Thanks

@eddiemonge eddiemonge changed the title When calling $state.go with notify = false resolve function is executed resolve is always called with notify:false Aug 19, 2015
@eddiemonge
Copy link
Contributor

Plenty of ways to replace the url but the original issue is still valid:

resolve is always called also with notify = false

@davideicardi
Copy link
Author

Thanks Eddie but what do you suggest for changing url?

For me it should be some ui-router feature, something like:

$state.go("mystate", { reload: false })

or

$state.change("mystate")

In my opinion is a state change just like any other, the only problem is that the user is actually already in this state (all the required data are loaded) so I just don't want to reload anything.

@eddiemonge
Copy link
Contributor

$location.path() with a $locationChangeStart set to preventDefault?

@nateabele
Copy link
Contributor

Yeah, see the example with the sync() method here: https://angular-ui.github.io/ui-router/site/#/api/ui.router.router.$urlRouter

@orangesoup
Copy link

@nateabele Shouldn't the example use $locationChangeStart instead of $locationChangeSuccess?

Also, it's not helping regarding the url change imo. If I don't use sync() the url won't change, but if I use it, it will just open the state fully, not just updating the url.

@calebeno
Copy link

calebeno commented Jun 2, 2016

@nateabele @eddiemonge I've been working on a project and having a similar issue as that discussed here. This is my use case: When transitioning to a state, a resolve block is completed. Once in the new state, a component determines whether or not the url needs to be changed. The goal is to remain in exactly the same state, but to modify the url. However, when I make the url modification using something like this:

$state.go($state.$current.self.name, {newparams}, {location: 'replace', reload: false, notify: false});

The entire state remains the same and the url is updated. However, the resolve block is the called again. Is it not possible to prevent the resolve block from running?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants