Skip to content

Commit 8612380

Browse files
committed
Merge pull request #591 from firetrust/master
Expose `$urlRouterUpdateStart` event to allow deferrable `update()`
2 parents 4175179 + 7fe4ad0 commit 8612380

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/urlRouter.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ function $UrlRouterProvider( $urlMatcherFactory) {
9292
[ '$location', '$rootScope', '$injector',
9393
function ($location, $rootScope, $injector) {
9494
// TODO: Optimize groups of rules with non-empty prefix into some sort of decision tree
95-
function update() {
95+
function update(evt) {
96+
if (evt && evt.defaultPrevented) return;
9697
function check(rule) {
9798
var handled = rule($injector, $location);
9899
if (handled) {
@@ -110,7 +111,12 @@ function $UrlRouterProvider( $urlMatcherFactory) {
110111
}
111112

112113
$rootScope.$on('$locationChangeSuccess', update);
113-
return {};
114+
115+
return {
116+
sync: function () {
117+
update();
118+
}
119+
};
114120
}];
115121
}
116122

test/urlRouterSpec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ describe("UrlRouter", function () {
8080
expect(custom.url.format).not.toHaveBeenCalled();
8181
expect(custom.handler).toHaveBeenCalled();
8282
});
83+
84+
it('can be cancelled by preventDefault() in $locationChangeSuccess', inject(function () {
85+
var called;
86+
location.path("/baz");
87+
scope.$on('$locationChangeSuccess', function (ev) {
88+
ev.preventDefault();
89+
called = true;
90+
});
91+
scope.$emit("$locationChangeSuccess");
92+
expect(called).toBeTruthy();
93+
expect(location.path()).toBe("/baz");
94+
}));
95+
96+
it('can be deferred and updated in $locationChangeSuccess', inject(function ($urlRouter, $timeout) {
97+
var called;
98+
location.path("/baz");
99+
scope.$on('$locationChangeSuccess', function (ev) {
100+
ev.preventDefault();
101+
called = true;
102+
$timeout($urlRouter.sync, 2000);
103+
});
104+
scope.$emit("$locationChangeSuccess");
105+
$timeout.flush();
106+
expect(called).toBeTruthy();
107+
expect(location.path()).toBe("/b4z");
108+
}));
83109
});
84110

85111
});

0 commit comments

Comments
 (0)