Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

$location support for pushState #1675

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 9 additions & 5 deletions src/ng/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,20 @@ function Browser(window, document, $log, $sniffer) {
*
* @param {string} url New url (when used as setter)
* @param {boolean=} replace Should new url replace current history record ?
* @param {object=} state object to use with pushState/replaceState
* @param {string=} title to use with pushState/replaceState
*/
self.url = function(url, replace) {
self.url = function(url, replace, state, title) {
// setter
if (url) {
if (lastBrowserUrl == url) return;
lastBrowserUrl = url;
if ($sniffer.history) {
if (replace) history.replaceState(null, '', url);
var title = title || ''
if(isUndefined(state)) state = null
if (replace) history.replaceState(state, title, url);
else {
history.pushState(null, '', url);
history.pushState(state, title, url);
// Crazy Opera Bug: http://my.opera.com/community/forums/topic.dml?id=1185462
baseElement.attr('href', baseElement.attr('href'));
}
Expand All @@ -173,12 +177,12 @@ function Browser(window, document, $log, $sniffer) {
var urlChangeListeners = [],
urlChangeInit = false;

function fireUrlChange() {
function fireUrlChange(ev) {
if (lastBrowserUrl == self.url()) return;

lastBrowserUrl = self.url();
forEach(urlChangeListeners, function(listener) {
listener(self.url());
listener(self.url(), ev);
});
}

Expand Down
46 changes: 41 additions & 5 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,40 @@ LocationUrl.prototype = {
return this;
},

/*
* @ngdoc method
* @name ng.$location#pushState
* @methodOf ng.$location
*
* @description
* @param {object=} state object for pushState
* @param {string=} title for pushState
* @param {string=} url New url without base prefix (e.g. `/path?a=b#hash`)
* @return {object} $location
*/
pushState: function(state, title, url, replace) {
this.$$state = state;
this.$$title = title;
this.url(url, replace)
return this;
},

/*
* @ngdoc method
* @name ng.$location#replaceState
* @methodOf ng.$location
*
* @description
* @param {object=} state object for replaceState
* @param {string=} title for pushState
* @param {string=} url New url without base prefix (e.g. `/path?a=b#hash`)
* @return {object} $location
*/
replaceState: function(state, title, url) {
this.pushState(state, title, url, true)
return this;
},

/**
* @ngdoc method
* @name ng.$location#protocol
Expand Down Expand Up @@ -574,13 +608,13 @@ function $LocationProvider(){
}

// update $location when $browser url changes
$browser.onUrlChange(function(newUrl) {
$browser.onUrlChange(function(newUrl, ev) {
if ($location.absUrl() != newUrl) {
$rootScope.$evalAsync(function() {
var oldUrl = $location.absUrl();

$location.$$parse(newUrl);
afterLocationChange(oldUrl);
afterLocationChange(oldUrl, ev);
});
if (!$rootScope.$$phase) $rootScope.$digest();
}
Expand All @@ -599,20 +633,22 @@ function $LocationProvider(){
defaultPrevented) {
$location.$$parse(oldUrl);
} else {
$browser.url($location.absUrl(), currentReplace);
$browser.url($location.absUrl(), $location.$$replace, $location.$$state, $location.$$title);
afterLocationChange(oldUrl);
}
});
}
$location.$$replace = false;
$location.$$state = null;
$location.$$title = null;

return changeCounter;
});

return $location;

function afterLocationChange(oldUrl) {
$rootScope.$broadcast('$locationChangeSuccess', $location.absUrl(), oldUrl);
function afterLocationChange(oldUrl, ev) {
$rootScope.$broadcast('$locationChangeSuccess', $location.absUrl(), oldUrl, ev);
}
}];
}