Skip to content

Commit

Permalink
jashkenas#419 - Added a global route event that can be subscribed to …
Browse files Browse the repository at this point in the history
…on the Backbone.history object. It accepts the following parameters: router, fragment and args.
  • Loading branch information
Irene Ros committed Oct 29, 2011
1 parent 2be7714 commit 17c30c5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
8 changes: 4 additions & 4 deletions backbone.js
Expand Up @@ -704,6 +704,7 @@
var args = this._extractParameters(route, fragment);
callback.apply(this, args);
this.trigger.apply(this, ['route:' + name].concat(args));
Backbone.history.trigger('route', this, fragment, args);
}, this));
},

Expand Down Expand Up @@ -763,7 +764,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 Expand Up @@ -856,12 +857,12 @@
// returns `false`.
loadUrl : function(fragmentOverride) {
var fragment = this.fragment = this.getFragment(fragmentOverride);
var matched = _.any(this.handlers, function(handler) {
var matched = _.any(this.handlers, _.bind(function(handler) {
if (handler.route.test(fragment)) {
handler.callback(fragment);
return true;
}
});
}, this));
return matched;
},

Expand All @@ -885,7 +886,6 @@
}
if (triggerRoute) this.loadUrl(fragment);
}

});

// Backbone.View
Expand Down
40 changes: 35 additions & 5 deletions test/router.js
Expand Up @@ -53,34 +53,64 @@ $(document).ready(function() {
equals(router.testing, 101);
});

asyncTest("Router: routes (simple)", 2, function() {
asyncTest("Router: routes (simple)", 4, function() {

Backbone.history.bind('route', function(r, fragment, args) {
equals(fragment, "search/news");
ok(_.isEqual(r, router));
});
window.location.hash = 'search/news';

setTimeout(function() {
equals(router.query, 'news');
equals(router.page, undefined);
Backbone.history.unbind('route');
start();
}, 10);
});

asyncTest("Router: routes (two part)", 2, function() {
asyncTest("Router: routes (two part)", 5, function() {
Backbone.history.bind('route', function(r, fragment, args) {
equals(fragment, 'search/nyc/p10');
ok(_.isEqual(args, ["nyc", "10"]));
ok(_.isEqual(r, router));
});
window.location.hash = 'search/nyc/p10';

setTimeout(function() {
equals(router.query, 'nyc');
equals(router.page, '10');
equals(router.query, 'nyc', "Should be nyc is actually " + router.query);
equals(router.page, '10', "Should be 10, is actually", router.page);
Backbone.history.unbind('route');
start();
}, 10);
});

test("Router: routes via navigate", 2, function() {
test("Router: routes via navigate", 5, function() {

Backbone.history.bind('route', function(r, fragment, args) {
equals(fragment, 'search/manhattan/p20');
ok(_.isEqual(args, ["manhattan", "20"]));
ok(_.isEqual(r, router));
});

Backbone.history.navigate('search/manhattan/p20', true);
Backbone.history.unbind('route');
equals(router.query, 'manhattan');
equals(router.page, '20');
});

asyncTest("Router: routes (splats)", function() {

Backbone.history.bind('route', function(r, fragment, args) {
equals(fragment, 'splat/long-list/of/splatted_99args/end');
ok(_.isEqual(args, ["long-list/of/splatted_99args"]));
ok(_.isEqual(r, router));
});

window.location.hash = 'splat/long-list/of/splatted_99args/end';
setTimeout(function() {
equals(router.args, 'long-list/of/splatted_99args');
Backbone.history.unbind('route');
start();
}, 10);
});
Expand Down

0 comments on commit 17c30c5

Please sign in to comment.