Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26c165f

Browse files
committed
push $location state to $browser in the $burp phase
instead of $digest
1 parent 9db62d6 commit 26c165f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/service/location.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ var URL_MATCH = /^(file|ftp|http|https):\/\/(\w+:{0,1}\w*@)?([\w\.-]*)(:([0-9]+)
6666
</doc:scenario>
6767
</doc:example>
6868
*/
69-
angularServiceInject("$location", function($browser) {
69+
angularServiceInject("$location", function($browser, $defer) {
7070
var location = {update: update, updateHash: updateHash};
7171
var lastLocation = {}; // last state since last update().
72+
var deferId;
7273

7374
$browser.onHashChange(bind(this, this.$apply, function() { //register
7475
update($browser.getUrl());
@@ -118,8 +119,10 @@ angularServiceInject("$location", function($browser) {
118119

119120
location.href = composeHref(location);
120121
}
121-
$browser.setUrl(location.href);
122122
copy(location, lastLocation);
123+
124+
$defer.cancel(deferId);
125+
deferId = $defer('$burp', function() { $browser.setUrl(location.href); });
123126
}
124127

125128
/**
@@ -270,4 +273,4 @@ angularServiceInject("$location", function($browser) {
270273

271274
return h;
272275
}
273-
}, ['$browser']);
276+
}, ['$browser', '$defer']);

test/service/locationSpec.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,20 @@ describe('$location', function() {
3939
});
4040

4141

42-
it('should update browser at the end of $eval', function() {
42+
it('should update browser at the end of $apply', function() {
4343
var origBrowserUrl = $browser.getUrl();
4444
$location.update('http://www.angularjs.org/');
4545
$location.update({path: '/a/b'});
4646
expect($location.href).toEqual('http://www.angularjs.org/a/b');
47-
expect($browser.getUrl()).toEqual('http://www.angularjs.org/a/b');
47+
expect($browser.getUrl()).toEqual('http://server');
4848
$location.path = '/c';
49+
50+
//don't flush stuff to the browser
4951
scope.$digest();
52+
expect($browser.getUrl()).toEqual('http://server');
53+
54+
//flush stuff to the browser
55+
scope.$service('$defer').flush('$burp');
5056
expect($browser.getUrl()).toEqual('http://www.angularjs.org/c');
5157
});
5258

0 commit comments

Comments
 (0)