added a test and code to allow a router to define a route with no callback method for the route name

This commit is contained in:
Derick Bailey
2011-10-06 19:09:53 -05:00
parent 41e470321d
commit 6799c8e8be
2 changed files with 22 additions and 1 deletions

View File

@@ -686,7 +686,9 @@
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
Backbone.history.route(route, _.bind(function(fragment) {
var args = this._extractParameters(route, fragment);
callback.apply(this, args);
if (callback){
callback.apply(this, args);
}
this.trigger.apply(this, ['route:' + name].concat(args));
}, this));
},

View File

@@ -5,6 +5,7 @@ $(document).ready(function() {
var Router = Backbone.Router.extend({
routes: {
"noCallback": "noCallback",
"search/:query": "search",
"search/:query/p:page": "search",
"splat/*args/end": "splat",
@@ -41,6 +42,8 @@ $(document).ready(function() {
this.anything = whatever;
}
// do not provide a callback method for the noCallback route
});
Backbone.history = null;
@@ -113,4 +116,20 @@ $(document).ready(function() {
}, 10);
});
asyncTest("Router: fires event when router doesn't have callback on it", 1, function() {
try{
var callbackFired = false;
var myCallback = function(){ callbackFired = true; }
router.bind("route:noCallback", myCallback);
window.location.hash = "noCallback";
setTimeout(function(){
equals(callbackFired, true);
start();
window.location.hash = '';
}, 10);
} catch (err) {
ok(false, "an exception was thrown trying to fire the router event with no router handler callback");
}
});
});