-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Return a promise from $location.path (and search) to detect if the change was cancelled #7392
Comments
How exactly would a location change fail? It doesn't make an http request or anything, so its not really clear |
@caitp I guess this is about the fact that the location change can be canceled. So it doesn't fail but can be prevented. It is true that in this case we are not propagating any location change error: |
Yeah but nothing in core cancels it, and presumably if a user cancels it, they know what they're doing. If a module cancels it, they might get another way, like |
Unfortunately route change error doesn't fire when it's cancelled either. Sent from my phone |
Because nothing ever cancels it, basically (it's really a situation where if you cancel it, you know that you cancelled it) |
What I'm saying is that if something cancels the location change, there is The problem is that we have a very large app, some ui gets shown, a user I've solved this by raising a custom event so the code that tried to change Sent from my phone |
@Shandem the other thing is that this is not an asynchronous operation at all, so a promise really doesn't make sense there |
But it must be async somewhere in there... If you set location.path the Sent from my phone |
The time between |
Sure, but what about between the operating of actually setting Sent from my phone |
@Shandem well, here's what happens. If the browser supports the history API, we're listening for navigation events (like |
Sorry didn't get time to put a snippet together for you today but this is // In module B: $scope.$on("$locationChangeStart", function(event, newPath, oldPath) { // In module A: var result = $location.path("/blah"); } But instead the execution continues in module A before the event handler Shannon Deminick On Fri, May 9, 2014 at 1:53 AM, Caitlin Potter [email protected]:
|
It wouldn't make sense to return a promise from |
Righto, no worries then, that's basically what I've done Sent from my phone |
(Obviously if the entire application is under your control it's easy to always trigger your own event. You can also work around it with |
It would be nice if $location.path('someUrl') would return a promise. If the $routeProvide resolve is rejected, then the promise would be rejected. location.path('/userSettings')
.then(function(){
//success
},function(error){
alert(error);//handles the rejection
} and angular.module('myApp')
.config(function ($routeProvider) {
$routeProvider
.when('/userSettings', {
templateUrl: 'app/routes/userSettings/userSettings.html',
controller: 'UserSettingsCtrl',
resolve:{
authenticatedUser: function(Auth){
return Auth.getAuthenticatedUserAsync(); //returns a promise
},
},
});
}); |
@honkskillet if Second problem is, who would resolve it if nobody actually cared about it? How should it work then? If it issued a promise that you could resolve/reject, but nobody did that? The major change that we could introduce to The problem is this cycle:
All is well even thought step 3 should fire after step 5. Now the main problem is when step 5 reads like this: at least one route.resolve promise fails which fires route change error event. Why is this a problem? Because URL has already changed to the new URL even though routing didn't get that far. So we end up with an old view with the new URL. Which is clearly wrong. And with So there is clearly a bug that's also outlined in #2100 (not resolved since beginning of 2013 when it was reported!!!). And that's the reason why @caitp I hope you understand why synchronous nature (or just invalid execution of |
Currently if you do a $location.path("/blah") (with optional .search) you have no way of knowing if someone has cancelled your attempt at changing the path by using a listener on $locationChangeStart + event.preventDefault()
Currently there is no event for $locationChangeFailed so it's near impossible to know if you path change attempt was cancelled without some hackery or custom events that are emitted in the code that is calling event.preventDefault().
The text was updated successfully, but these errors were encountered: