Skip to content

Commit

Permalink
Merge branch 'replaceState' of https://github.com/lmp/backbone
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Nov 23, 2011
2 parents f5acb6d + df7c279 commit 30a89ac
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 173 deletions.
49 changes: 38 additions & 11 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,8 @@
},

// Simple proxy to `Backbone.history` to save a fragment into the history.
navigate : function(fragment, triggerRoute) {
Backbone.history.navigate(fragment, triggerRoute);
navigate : function(fragment, options) {
Backbone.history.navigate(fragment, options);
},

// Bind all defined routes to `Backbone.history`. We have to reverse the
Expand Down Expand Up @@ -857,27 +857,54 @@
return matched;
},

// Save a fragment into the hash history. You are responsible for properly
// Save a fragment into the hash history, or replace the URL state
// if the 'replace' option is passed. You are responsible for properly
// URL-encoding the fragment in advance. This does not trigger
// a `hashchange` event.
navigate : function(fragment, triggerRoute) {
// parameters:
//
// * fragment: the URL fragment to navigate to (the portion after the '#')
// * options: An object with the following parameters:
// - trigger: call the route corresponding to the provided fragment
// - replace: Navigate such that the back button will
// not return to this current state.
//
// To comply with earlier API specifications, passing
// true/false for options will be interpretted as
// {options: trigger: true/false}
navigate : function(fragment, options) {
if (!options || typeof options === 'boolean') options = {trigger: options};
var frag = (fragment || '').replace(hashStrip, '');
if (this.fragment == frag || this.fragment == decodeURIComponent(frag)) return;
if (this._hasPushState) {
var loc = window.location;
if (frag.indexOf(this.options.root) != 0) frag = this.options.root + frag;
this.fragment = frag;
window.history.pushState({}, document.title, loc.protocol + '//' + loc.host + frag);
if (options.replace) {
window.history.replaceState({}, document.title, frag);
} else {
window.history.pushState({}, document.title, frag);
}
} else {
window.location.hash = this.fragment = frag;
this.fragment = frag;
this._updateLocationHash(window.location, frag, options.replace);
if (this.iframe && (frag != this.getFragment(this.iframe.location.hash))) {
this.iframe.document.open().close();
this.iframe.location.hash = frag;
// Opening and closing the iframe tricks IE7 and earlier to push a history entry on hash-tag change.
// When replace is true, we don't want this.
if(!options.replace) this.iframe.document.open().close();
this._updateLocationHash(this.iframe.location, frag, options.replace);
}
}
if (triggerRoute) this.loadUrl(fragment);
}
if (options.trigger) this.loadUrl(fragment);
},

// Since you can't run `location.replace` on a hash fragment, this
// helper function provides an effective work-around.
_updateLocationHash: function(location, new_fragment, replace) {
if (replace)
location.replace(location.toString().replace(/#.*$/, "") + "#" + new_fragment);
else
location.hash = new_fragment;
}
});

// Backbone.View
Expand Down
Loading

0 comments on commit 30a89ac

Please sign in to comment.