Skip to content
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

Only push new history when initialURL has changed #1094

Merged
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
19 changes: 14 additions & 5 deletions packages/ember-application/lib/system/history_location.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ var get = Ember.get, set = Ember.set;
Ember.HistoryLocation = Ember.Object.extend({
init: function() {
set(this, 'location', get(this, 'location') || window.location);
set(this, '_initialURL', get(this, 'location').pathname);
set(this, 'callbacks', Ember.A());
},

/**
@private

Used to give history a starting reference
*/
_initialURL: null,

/**
@private

Expand All @@ -25,12 +33,13 @@ Ember.HistoryLocation = Ember.Object.extend({
Uses `history.pushState` to update the url without a page reload.
*/
setURL: function(path) {
var state = window.history.state;
var state = window.history.state,
initialURL = get(this, '_initialURL');

if (path === "") { path = '/'; }
// We only want pushState to be executed if we are passing
// in a new path, otherwise a new state will be inserted
// for the same path.
if ((!state && path !== '/') || (state && state.path !== path)) {

if ((initialURL && initialURL !== path) || (state && state.path !== path)) {
set(this, '_initialURL', null);
window.history.pushState({ path: path }, null, path);
}
},
Expand Down
4 changes: 2 additions & 2 deletions packages/ember-application/tests/system/location_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ test("if history is used, it triggers the popstate event", function() {
window.history.back();
});

test("doesn't push a state if there is no state and path is '/'", function() {
test("doesn't push a state if path has not changed", function() {
expect(1);
stop();

Expand All @@ -147,5 +147,5 @@ test("doesn't push a state if there is no state and path is '/'", function() {
equal(count, 0, "pushState should not have been called");
}, 100);

locationObject.setURL('/');
locationObject.setURL(window.location.pathname);
});