angular / router

The Angular 1 Component Router
MIT License
665 stars 135 forks source link

Router.Navigate ignoring canDeactivate Failures #422

Open akasapu2985 opened 8 years ago

akasapu2985 commented 8 years ago

in Router.navigate method, even if the process.then goes to error case, we are not rejecting the promise, and setting the instruction.canonical form. which causes location.path to change, even if the canDeactivate returns false.

Issue: this is causing the navigation to happen twice. And also url changes even if I returned false in canDeactivate.

return this.pipeline.process(instruction).then((function() {
        return $__0._finishNavigating();
      }), (function() {
        return $__0._finishNavigating();
      })).then((function() {
        return instruction.canonicalUrl;
      }));
    },
return nav.call(this, url).then(function (newUrl) {
      if (newUrl) {
        $location.path(newUrl);
      }
    });

when the pipeline.Process fails(canDeactivate returns false), we should finishNavigating but also reject the promise.

my fix for this is

var processDefer = $q.defer();
    this.pipeline.process(instruction).then((function () {
        $__0._finishNavigating();
        instruction.canonicalUrl;
        processDefer.resolve();
    }), (function () {
        $__0._finishNavigating();
        processDefer.reject();
    }));