diff --git a/backbone.js b/backbone.js index ebb04979..d0f49e1f 100644 --- a/backbone.js +++ b/backbone.js @@ -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)); }, diff --git a/test/router.js b/test/router.js index 2562b0c6..4e840232 100644 --- a/test/router.js +++ b/test/router.js @@ -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"); + } + }); + });