Skip to content

Commit

Permalink
Merge pull request #2659 from braddunbar/trailing-root
Browse files Browse the repository at this point in the history
Fix #2656 - Trailing slash on root.
  • Loading branch information
braddunbar committed Jul 6, 2013
2 parents 25f3949 + 15b7d77 commit 9e0f188
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1472,11 +1472,16 @@
navigate: function(fragment, options) {
if (!History.started) return false;
if (!options || options === true) options = {trigger: !!options};

fragment = this.getFragment(fragment || '');
if (this.fragment === fragment) return;
this.fragment = fragment;

var url = this.root + fragment;

// Don't include a trailing slash on the root.
if (fragment === '' && url !== '/') url = url.slice(0, -1);

// If pushState is available, we use it to set the fragment as a real URL.
if (this._hasPushState) {
this.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, url);
Expand Down
30 changes: 30 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,4 +656,34 @@ $(document).ready(function() {
Backbone.history.checkUrl();
});

test('#2656 - No trailing slash on root.', 1, function() {
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url){
strictEqual(url, '/root');
}
}
});
location.replace('http://example.com/root/path');
Backbone.history.start({pushState: true, root: 'root'});
Backbone.history.navigate('');
});

test('#2656 - No trailing slash on root.', 1, function() {
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url){
strictEqual(url, '/');
}
}
});
location.replace('http://example.com/path');
Backbone.history.start({pushState: true});
Backbone.history.navigate('');
});

});

0 comments on commit 9e0f188

Please # to comment.