Skip to content

Commit

Permalink
Merge pull request #1658 from braddunbar/history-window
Browse files Browse the repository at this point in the history
Fix #1653 - Ensure that `History` can be used outside of the browser.
  • Loading branch information
jashkenas committed Sep 17, 2012
2 parents da18e00 + a424341 commit 7bfce6f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
10 changes: 7 additions & 3 deletions backbone.js
Expand Up @@ -971,11 +971,15 @@


// Handles cross-browser history management, based on URL fragments. If the // Handles cross-browser history management, based on URL fragments. If the
// browser does not support `onhashchange`, falls back to polling. // browser does not support `onhashchange`, falls back to polling.
var History = Backbone.History = function(options) { var History = Backbone.History = function() {
this.handlers = []; this.handlers = [];
_.bindAll(this, 'checkUrl'); _.bindAll(this, 'checkUrl');
this.location = options && options.location || root.location;
this.history = options && options.history || root.history; // #1653 - Ensure that `History` can be used outside of the browser.
if (typeof window !== 'undefined') {
this.location = window.location;
this.history = window.history;
}
}; };


// Cached regex for cleaning leading hashes and slashes. // Cached regex for cleaning leading hashes and slashes.
Expand Down
30 changes: 15 additions & 15 deletions test/router.js
Expand Up @@ -41,7 +41,7 @@ $(document).ready(function() {


setup: function() { setup: function() {
location = new Location('http://example.com'); location = new Location('http://example.com');
Backbone.history = new Backbone.History({location: location}); Backbone.history = _.extend(new Backbone.History, {location: location});
router = new Router({testing: 101}); router = new Router({testing: 101});
Backbone.history.interval = 9; Backbone.history.interval = 9;
Backbone.history.start({pushState: false}); Backbone.history.start({pushState: false});
Expand Down Expand Up @@ -233,12 +233,12 @@ $(document).ready(function() {
location.replace('http://example.com/root/foo'); location.replace('http://example.com/root/foo');


Backbone.history.stop(); Backbone.history.stop();
Backbone.history = new Backbone.History({location: location}); Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({root: '/root', hashChange: false, silent: true}); Backbone.history.start({root: '/root', hashChange: false, silent: true});
strictEqual(Backbone.history.getFragment(), 'foo'); strictEqual(Backbone.history.getFragment(), 'foo');


Backbone.history.stop(); Backbone.history.stop();
Backbone.history = new Backbone.History({location: location}); Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({root: '/root/', hashChange: false, silent: true}); Backbone.history.start({root: '/root/', hashChange: false, silent: true});
strictEqual(Backbone.history.getFragment(), 'foo'); strictEqual(Backbone.history.getFragment(), 'foo');
}); });
Expand Down Expand Up @@ -272,7 +272,7 @@ $(document).ready(function() {
test("#1185 - Use pathname when hashChange is not wanted.", 1, function() { test("#1185 - Use pathname when hashChange is not wanted.", 1, function() {
Backbone.history.stop(); Backbone.history.stop();
location.replace('http://example.com/path/name#hash'); location.replace('http://example.com/path/name#hash');
Backbone.history = new Backbone.History({location: location}); Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({hashChange: false}); Backbone.history.start({hashChange: false});
var fragment = Backbone.history.getFragment(); var fragment = Backbone.history.getFragment();
strictEqual(fragment, location.pathname.replace(/^\//, '')); strictEqual(fragment, location.pathname.replace(/^\//, ''));
Expand All @@ -281,7 +281,7 @@ $(document).ready(function() {
test("#1206 - Strip leading slash before location.assign.", 1, function() { test("#1206 - Strip leading slash before location.assign.", 1, function() {
Backbone.history.stop(); Backbone.history.stop();
location.replace('http://example.com/root/'); location.replace('http://example.com/root/');
Backbone.history = new Backbone.History({location: location}); Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({hashChange: false, root: '/root/'}); Backbone.history.start({hashChange: false, root: '/root/'});
location.assign = function(pathname) { location.assign = function(pathname) {
strictEqual(pathname, '/root/fragment'); strictEqual(pathname, '/root/fragment');
Expand All @@ -292,15 +292,15 @@ $(document).ready(function() {
test("#1387 - Root fragment without trailing slash.", 1, function() { test("#1387 - Root fragment without trailing slash.", 1, function() {
Backbone.history.stop(); Backbone.history.stop();
location.replace('http://example.com/root'); location.replace('http://example.com/root');
Backbone.history = new Backbone.History({location: location}); Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({hashChange: false, root: '/root/', silent: true}); Backbone.history.start({hashChange: false, root: '/root/', silent: true});
strictEqual(Backbone.history.getFragment(), ''); strictEqual(Backbone.history.getFragment(), '');
}); });


test("#1366 - History does not prepend root to fragment.", 2, function() { test("#1366 - History does not prepend root to fragment.", 2, function() {
Backbone.history.stop(); Backbone.history.stop();
location.replace('http://example.com/root/'); location.replace('http://example.com/root/');
Backbone.history = new Backbone.History({ Backbone.history = _.extend(new Backbone.History, {
location: location, location: location,
history: { history: {
pushState: function(state, title, url) { pushState: function(state, title, url) {
Expand All @@ -320,7 +320,7 @@ $(document).ready(function() {
test("Normalize root.", 1, function() { test("Normalize root.", 1, function() {
Backbone.history.stop(); Backbone.history.stop();
location.replace('http://example.com/root'); location.replace('http://example.com/root');
Backbone.history = new Backbone.History({ Backbone.history = _.extend(new Backbone.History, {
location: location, location: location,
history: { history: {
pushState: function(state, title, url) { pushState: function(state, title, url) {
Expand All @@ -339,7 +339,7 @@ $(document).ready(function() {
test("Normalize root.", 1, function() { test("Normalize root.", 1, function() {
Backbone.history.stop(); Backbone.history.stop();
location.replace('http://example.com/root#fragment'); location.replace('http://example.com/root#fragment');
Backbone.history = new Backbone.History({ Backbone.history = _.extend(new Backbone.History, {
location: location, location: location,
history: { history: {
pushState: function(state, title, url) {}, pushState: function(state, title, url) {},
Expand All @@ -357,7 +357,7 @@ $(document).ready(function() {
test("Normalize root.", 1, function() { test("Normalize root.", 1, function() {
Backbone.history.stop(); Backbone.history.stop();
location.replace('http://example.com/root'); location.replace('http://example.com/root');
Backbone.history = new Backbone.History({location: location}); Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.loadUrl = function() { ok(true); }; Backbone.history.loadUrl = function() { ok(true); };
Backbone.history.start({ Backbone.history.start({
pushState: true, pushState: true,
Expand All @@ -368,7 +368,7 @@ $(document).ready(function() {
test("Normalize root - leading slash.", 1, function() { test("Normalize root - leading slash.", 1, function() {
Backbone.history.stop(); Backbone.history.stop();
location.replace('http://example.com/root'); location.replace('http://example.com/root');
Backbone.history = new Backbone.History({ Backbone.history = _.extend(new Backbone.History, {
location: location, location: location,
history: { history: {
pushState: function(){}, pushState: function(){},
Expand All @@ -382,7 +382,7 @@ $(document).ready(function() {
test("Transition from hashChange to pushState.", 1, function() { test("Transition from hashChange to pushState.", 1, function() {
Backbone.history.stop(); Backbone.history.stop();
location.replace('http://example.com/root#x/y'); location.replace('http://example.com/root#x/y');
Backbone.history = new Backbone.History({ Backbone.history = _.extend(new Backbone.History, {
location: location, location: location,
history: { history: {
pushState: function(){}, pushState: function(){},
Expand All @@ -400,7 +400,7 @@ $(document).ready(function() {
test("#1619: Router: Normalize empty root", 1, function() { test("#1619: Router: Normalize empty root", 1, function() {
Backbone.history.stop(); Backbone.history.stop();
location.replace('http://example.com/'); location.replace('http://example.com/');
Backbone.history = new Backbone.History({ Backbone.history = _.extend(new Backbone.History, {
location: location, location: location,
history: { history: {
pushState: function(){}, pushState: function(){},
Expand All @@ -414,7 +414,7 @@ $(document).ready(function() {
test("#1619: Router: nagivate with empty root", 1, function() { test("#1619: Router: nagivate with empty root", 1, function() {
Backbone.history.stop(); Backbone.history.stop();
location.replace('http://example.com/'); location.replace('http://example.com/');
Backbone.history = new Backbone.History({ Backbone.history = _.extend(new Backbone.History, {
location: location, location: location,
history: { history: {
pushState: function(state, title, url) { pushState: function(state, title, url) {
Expand All @@ -436,7 +436,7 @@ $(document).ready(function() {
location.replace = function(url) { location.replace = function(url) {
strictEqual(url, '/root/?a=b#x/y'); strictEqual(url, '/root/?a=b#x/y');
}; };
Backbone.history = new Backbone.History({ Backbone.history = _.extend(new Backbone.History, {
location: location, location: location,
history: { history: {
pushState: null, pushState: null,
Expand Down

0 comments on commit 7bfce6f

Please sign in to comment.