Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Fixes jashkenas#538 -- adds Backbone.history.stop() for unit testers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Jan 23, 2012
1 parent 36fde54 commit 96a7274
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 10 additions & 1 deletion backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@
} else if (this._wantsHashChange && ('onhashchange' in window) && !oldIE) {
$(window).bind('hashchange', this.checkUrl);
} else if (this._wantsHashChange) {
setInterval(this.checkUrl, this.interval);
this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
}

// Determine if we need to change the base url, for a pushState link
Expand All @@ -860,6 +860,14 @@
}
},

// Disable Backbone.history, perhaps temporarily. Not useful in a real app,
// but possibly useful for unit testing Routers.
stop: function() {
$(window).unbind('popstate', this.checkUrl).unbind('hashchange', this.checkUrl);
clearInterval(this._checkUrlInterval);
historyStarted = false;
},

// Add a route to be tested when the fragment changes. Routes added later
// may override previous routes.
route: function(route, callback) {
Expand Down Expand Up @@ -898,6 +906,7 @@
// route callback be fired (not usually desirable), or `replace: true`, if
// you which to modify the current URL without adding an entry to the history.
navigate: function(fragment, options) {
if (!historyStarted) return false;
if (!options || options === true) options = {trigger: options};
var frag = (fragment || '').replace(routeStripper, '');
if (this.fragment == frag || this.fragment == decodeURIComponent(frag)) return;
Expand Down
8 changes: 7 additions & 1 deletion test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,17 @@ $(document).ready(function() {
router.navigate('search/counter', {trigger: true});
router.navigate('counter', {trigger: true});
equal(router.count, 2);
Backbone.history.stop();
router.navigate('search/counter', {trigger: true});
router.navigate('counter', {trigger: true});
equal(router.count, 2);
Backbone.history.start();
equal(router.count, 3);
});

test("Router: use implicit callback if none provided", function() {
router.count = 0;
router.navigate('implicit', {trigger: true})
router.navigate('implicit', {trigger: true});
equal(router.count, 1);
});

Expand Down

0 comments on commit 96a7274

Please sign in to comment.