Skip to content

Expose $urlRouterUpdateStart event to allow deferrable update() #591

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

Merged
merged 1 commit into from
Nov 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/urlRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ function $UrlRouterProvider( $urlMatcherFactory) {
[ '$location', '$rootScope', '$injector',
function ($location, $rootScope, $injector) {
// TODO: Optimize groups of rules with non-empty prefix into some sort of decision tree
function update() {
function update(evt) {
if (evt && evt.defaultPrevented) return;
function check(rule) {
var handled = rule($injector, $location);
if (handled) {
Expand All @@ -110,7 +111,12 @@ function $UrlRouterProvider( $urlMatcherFactory) {
}

$rootScope.$on('$locationChangeSuccess', update);
return {};

return {
sync: function () {
update();
}
};
}];
}

Expand Down
26 changes: 26 additions & 0 deletions test/urlRouterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ describe("UrlRouter", function () {
expect(custom.url.format).not.toHaveBeenCalled();
expect(custom.handler).toHaveBeenCalled();
});

it('can be cancelled by preventDefault() in $locationChangeSuccess', inject(function () {
var called;
location.path("/baz");
scope.$on('$locationChangeSuccess', function (ev) {
ev.preventDefault();
called = true;
});
scope.$emit("$locationChangeSuccess");
expect(called).toBeTruthy();
expect(location.path()).toBe("/baz");
}));

it('can be deferred and updated in $locationChangeSuccess', inject(function ($urlRouter, $timeout) {
var called;
location.path("/baz");
scope.$on('$locationChangeSuccess', function (ev) {
ev.preventDefault();
called = true;
$timeout($urlRouter.sync, 2000);
});
scope.$emit("$locationChangeSuccess");
$timeout.flush();
expect(called).toBeTruthy();
expect(location.path()).toBe("/b4z");
}));
});

});