-
Notifications
You must be signed in to change notification settings - Fork 27.5k
feat($location): Location change notifications can now be skipped #2398
Conversation
This adds a new method `notify` to `$location` that allows updating the location without triggering `$locationChangeStart`/`$locationChangeSuccess` events. The method is chainable and must be called with a boolean parameter. Any falsy value will disable the notification procedure and will block any route update that may apply. The skip flag will be reset after any digest cycle, so it must be set again on any subsequent change if needed. Example: `$location.path('/client/2').replace().notify(false);` Closes angular#1699
I've already signed the CLA. This feature adds 114 bytes to the minified build. Issue #1699 describes the scenario that inspired this PR. The original idea came from @qualidafial. Tests and documentation changes are also included. |
|
-1 I talked to peter and explained to him how this breaks routing. he'll provide more details shortly |
Ok! Let's see what's going on... |
Hi @lrlopez Thanks once again for the work that you put into this PR. You did all the right things with the implementation, style, tests, docs, CLA, and so on. As Igor mentioned, we discussed this PR yesterday. The problem is that if you use the $route service then you are signing up to a way of running your app, where the URL represents a specific route. By allowing people to change the URL, without changing the route, you are subverting the way that the routing system works. The result is that you are then in an application state where the URL indicates a particular route is running but actually a different route is in place. It may be that you are able in your own application to manage this inconsistency but as a general rule it will likely lead to people abusing it or just getting themselves into trouble and creating difficult to identify bugs. So where does this leave us for your particular issue? The simplest solution is to change routes when you have saved the item, changing the URL and giving the application the chance to get itself into the correct state. Your concern with this is that when you change route from mything/new to mything/1234, the controller of the second route will reload all the data of the item that you have just created and so this is wasteful of bandwidth or visual performance (a flicker of unwanted reload)? You could get around the former by using a service to store the current mything, which is checked when you enter the mything/:id route. If the current mything is the same as requested no server call need be made. This way you keep the controller as light as possible. The latter, is more problematic in the current form of the routing service. Since the route will change, the ng-view will be destroyed and reloaded. It is possible that you could get around this by doing some manual work. For instance, you could try having a more general route for mythings then dealing with the edit/new state yourself, by watching the location path and using ng-includes. Also, it might be worth taking a look at the AngularUI ui-router project, https://github.com/angular-ui/ui-router, which is more configurable and flexible, although still immature, and may be able to support what you need more easily. I am going to close this PR for now. If you have further ideas or feel that we have come to the wrong conclusion the do please convince us otherwise of the use and suitability. Thanks |
I understand. Anyway, I'll leave the feature branch on my repository so it can be used by anyone who needs this. Just drop me a line if it gets unmergable and I'll rebase to latest master. |
Would be great if you could reconsider this PR as this is really required for mobile applications where the ng-view create/destroy lifecycle is not appropriate (it kills performance and usability). Flexibility isn't a bad thing and clearly the current |
Is there any reason why you can't make $location update the route information if the reload is suppressed? |
ngRoute is now a separate module from the core download (in 1.1.x). It has On 12 July 2013 03:25, Cameron McKay notifications@github.com wrote:
|
I read several issues yesterday but I don't understand as an AngularJs user what exactly is implemented and how should I achieve the functionality of changing just the URL without reloading. What should be my final implementation? Yesterday I used $state.go('someState', {notify: false, location: replace}); but I'd rather just change location if possible. |
@NaomiN you cannot do it with ngRoute. You may be able to do it with ui-router (which I notice you are using given the use of |
I used switch to another state. Seems to be working fine. |
This adds a new
notify
method to$location
that allows updating the location without triggering$locationChangeStart
/$locationChangeSuccess
events.The method is chainable and must be called with a boolean parameter. Any falsy value will disable the notification procedure and will block any route update that may apply.
The skip flag will be reset after any digest cycle, so it must be set again on any subsequent change if needed.
Example:
$location.path('/client/2').replace().notify(false);
Closes #1699