Skip to content

Commit

Permalink
Fixes #419 -- add a global 'route' event, on Bacckbone.history.
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Jan 13, 2012
1 parent a78be62 commit 5b43cd9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 2 additions & 1 deletion backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@
var args = this._extractParameters(route, fragment);
callback && callback.apply(this, args);
this.trigger.apply(this, ['route:' + name].concat(args));
Backbone.history.trigger('route', this, name, args);
}, this));
},

Expand Down Expand Up @@ -728,7 +729,7 @@
var historyStarted = false;

// Set up all inheritable **Backbone.History** properties and methods.
_.extend(Backbone.History.prototype, {
_.extend(Backbone.History.prototype, Backbone.Events, {

// The default interval to poll for hash changes, if necessary, is
// twenty times a second.
Expand Down
16 changes: 14 additions & 2 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,24 @@ $(document).ready(function() {
Backbone.history.interval = 9;
Backbone.history.start({pushState: false});

var lastRoute = null;
var lastArgs = [];
Backbone.history.bind('route', function(router, route, args) {
lastRoute = route;
lastArgs = args;
});

test("Router: initialize", function() {
equals(router.testing, 101);
});

asyncTest("Router: routes (simple)", 2, function() {
asyncTest("Router: routes (simple)", 4, function() {
window.location.hash = 'search/news';
setTimeout(function() {
equals(router.query, 'news');
equals(router.page, undefined);
equals(lastRoute, 'search');
equals(lastArgs[0], 'news');
start();
}, 10);
});
Expand Down Expand Up @@ -137,11 +146,14 @@ $(document).ready(function() {
}, 10);
});

asyncTest("Router: routes (query)", 2, function() {
asyncTest("Router: routes (query)", 5, function() {
window.location.hash = 'mandel?a=b&c=d';
setTimeout(function() {
equals(router.entity, 'mandel');
equals(router.queryArgs, 'a=b&c=d');
equals(lastRoute, 'query');
equals(lastArgs[0], 'mandel');
equals(lastArgs[1], 'a=b&c=d');
start();
}, 10);
});
Expand Down

0 comments on commit 5b43cd9

Please sign in to comment.