[Pkg-javascript-commits] [backbone] 69/211: Changing route order behavior. Issue #189. Declarative route maps are the same, procedural route() calls can now override previously-defined routes.
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 17:00:04 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.5.0
in repository backbone.
commit ac8c3dd9db88257b356744ece4f6d12f0ce4997d
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Mon Feb 7 11:10:56 2011 -0500
Changing route order behavior. Issue #189. Declarative route maps are the same, procedural route() calls can now override previously-defined routes.
---
backbone.js | 17 +++++++++++------
test/controller.js | 15 ++++++++++++++-
2 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/backbone.js b/backbone.js
index 2a0deb9..f7ba05c 100644
--- a/backbone.js
+++ b/backbone.js
@@ -670,12 +670,17 @@
Backbone.history.saveLocation(fragment);
},
- // Bind all defined routes to `Backbone.history`.
+ // Bind all defined routes to `Backbone.history`. We have to reverse the
+ // order of the routes here to support behavior where the most general
+ // routes can be defined at the bottom of the route map.
_bindRoutes : function() {
if (!this.routes) return;
+ var routes = [];
for (var route in this.routes) {
- var name = this.routes[route];
- this.route(route, name, this[name]);
+ routes.unshift([route, this.route[route]]);
+ }
+ for (var i = 0, l = routes.length; i < l; i++) {
+ this.route(routes[0], routes[1], this[routes[1]]);
}
},
@@ -743,10 +748,10 @@
return this.loadUrl();
},
- // Add a route to be tested when the hash changes. Routes are matched in the
- // order they are added.
+ // Add a route to be tested when the hash changes. Routes added later may
+ // override previous routes.
route : function(route, callback) {
- this.handlers.push({route : route, callback : callback});
+ this.handlers.unshift({route : route, callback : callback});
},
// Checks the current URL to see if it has changed, and if it has,
diff --git a/test/controller.js b/test/controller.js
index 7f43fb6..4704f87 100644
--- a/test/controller.js
+++ b/test/controller.js
@@ -9,7 +9,8 @@ $(document).ready(function() {
"search/:query/p:page": "search",
"splat/*args/end": "splat",
"*first/complex-:part/*rest": "complex",
- ":entity?*args": "query"
+ ":entity?*args": "query",
+ "*anything": "anything"
},
initialize : function(options) {
@@ -34,6 +35,10 @@ $(document).ready(function() {
query : function(entity, args) {
this.entity = entity;
this.queryArgs = args;
+ },
+
+ anything : function(whatever) {
+ this.anything = whatever;
}
});
@@ -89,6 +94,14 @@ $(document).ready(function() {
equals(controller.entity, 'mandel');
equals(controller.queryArgs, 'a=b&c=d');
start();
+ }, 10);
+ });
+
+ asyncTest("Controller: routes (anything)", 1, function() {
+ window.location.hash = 'doesnt-match-a-route';
+ setTimeout(function() {
+ equals(controller.anything, 'doesnt-match-a-routea');
+ start();
window.location.hash = '';
}, 10);
});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git
More information about the Pkg-javascript-commits
mailing list