Skip to content

Commit

Permalink
Allow replaceState via Location.replaceState #4
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Sep 17, 2014
1 parent 6105b2e commit 4e0fe37
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions examples/historyState/historyState.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ if (Meteor.isClient) {
Template.hello.events({
'submit #push': function(e, t) {
e.preventDefault();
Iron.Location.go('/' + Random.id(), {historyState: t.$('input').val()});
var state = $(e.target).find('input').val();
Iron.Location.go('/' + Random.id(), {historyState: state});
},
'submit #replace': function(e, t) {
e.preventDefault();
console.log("Can't do this yet");
var state = $(e.target).find('input').val();
Iron.Location.replaceState(state);
}
})
}
22 changes: 22 additions & 0 deletions lib/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ var onClickHandler = function (e) {
throw err;
}
};

var replaceState = function(historyState) {
// XXX: should we throw / warn / do nothing here?
if (isUsingHashPaths())

This comment has been minimized.

Copy link
@cmather

cmather Sep 17, 2014

Contributor

Definitely shouldn't throw an error here.

If you call history.replaceState does it fire the popstate event? Otherwise, I think you should also be setting or updating the state object you get from calling Location.get(). So the idea of using history is sort of optional, but what you get from calling the get() method is always consistent.

This comment has been minimized.

Copy link
@tmeasday

tmeasday Sep 18, 2014

Author Contributor

So in the case of hashpaths you could still see it in Iron.Location.current.options.historyState (uggh, better ideas for names here?) but it wouldn't be "permanent" in the sense of being saved to history?

throw new Error("Can't replaceState when using hash paths");

var state = current;
if (EJSON.equals(state.options.historyState, historyState))
return;

state.options.historyState = historyState;
history.replaceState(historyState, null, state.url);
dep.changed();
}

/*****************************************************************************/
/* Location API */
/*****************************************************************************/
Expand Down Expand Up @@ -228,6 +243,13 @@ Location.go = function (url, options) {
return go(url, options);
};

/**
* Update the history.state for the given location
*/
Location.replaceState = function(historyState) {
return replaceState(historyState);
}

/**
* Automatically start Iron.Location
*/
Expand Down

0 comments on commit 4e0fe37

Please sign in to comment.