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 Oct 20, 2014
1 parent 8716ff7 commit 537426f
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 @@ -125,6 +125,21 @@ var onClickHandler = function (e) {
throw err;
}
};

var replaceState = function(historyState) {
// XXX: should we throw / warn / do nothing here?
if (isUsingHashPaths())
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 @@ -232,6 +247,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 537426f

Please sign in to comment.