Skip to content

Commit ee5d663

Browse files
ofirdaganShahar Talmi
authored and
Shahar Talmi
committed
feat(ng-widget): notify old url on location change events
Closes #24
1 parent 7102234 commit ee5d663

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

app/scripts/app.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ angular.module('angularWidget', ['angularWidgetInternal'])
1111
//an internal route changes, effecting only the router inside that view.
1212
$provide.decorator('$rootScope', function ($delegate, $injector) {
1313
var next, last, originalBroadcast = $delegate.$broadcast;
14+
var lastAbsUrl = '';
1415

1516
//sending $locationChangeSuccess will cause another $routeUpdate
1617
//so we need this ugly flag to prevent call stack overflow
1718
var suspendListener = false;
1819

1920
function suspendedNotify(widgets, $location) {
2021
suspendListener = true;
21-
widgets.notifyWidgets('$locationChangeStart', $location.absUrl(), '');
22-
widgets.notifyWidgets('$locationChangeSuccess', $location.absUrl(), '');
22+
var absUrl = $location.absUrl();
23+
widgets.notifyWidgets('$locationChangeStart', absUrl, lastAbsUrl);
24+
widgets.notifyWidgets('$locationChangeSuccess', absUrl, lastAbsUrl);
25+
lastAbsUrl = absUrl;
2326
suspendListener = false;
2427
}
2528

@@ -91,7 +94,7 @@ angular.module('angularWidgetOnly', [])
9194
.run(function ($rootScope, $location) {
9295
//widget - since $location is shared and is not going to be instantiated
9396
//by the new injector of this widget, we send the $locationChangeSuccess
94-
//ourselves to kickoff ng-rounte and ui-router ($location usually does that
97+
//ourselves to kickoff ng-route and ui-router ($location usually does that
9598
//itself during instantiation)
9699
$rootScope.$evalAsync(function () {
97100
var ev = $rootScope.$broadcast('$locationChangeStart', $location.absUrl(), '');

test/spec/app.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ describe('Unit testing routing hacks', function () {
5757
expect(notifyWidgets).toHaveBeenCalledWith('$locationChangeSuccess', 'http://server/', '');
5858
}));
5959

60+
it('should notify location change with old url', inject(function ($rootScope, $location) {
61+
$rootScope.$broadcast('$routeUpdate');
62+
expect(notifyWidgets).toHaveBeenCalledWith('$locationChangeStart', 'http://server/', '');
63+
expect(notifyWidgets).toHaveBeenCalledWith('$locationChangeSuccess', 'http://server/', '');
64+
65+
$location.path('/bla');
66+
$rootScope.$broadcast('$routeUpdate');
67+
68+
expect(notifyWidgets).toHaveBeenCalledWith('$locationChangeStart', 'http://server/#/bla', 'http://server/');
69+
expect(notifyWidgets).toHaveBeenCalledWith('$locationChangeSuccess', 'http://server/#/bla', 'http://server/');
70+
}));
71+
6072
it('should mute route change if widget did not change', inject(function ($rootScope) {
6173
var eventSpy = jasmine.createSpy('$routeChangeSuccess');
6274
var eventSpyMuted = jasmine.createSpy('$routeChangeMuted');

0 commit comments

Comments
 (0)