Permalink
Browse files

fixes #36 uses current route wins if all else equal

  • Loading branch information...
justinbmeyer committed May 10, 2012
1 parent 58bfb2e commit 863f37cc3d34f52517050444e0b31b7d63d6c784
Showing with 41 additions and 19 deletions.
  1. +20 −19 route/route.js
  2. +21 −0 route/route_test.js
View
@@ -118,25 +118,27 @@ steal('can/observe', 'can/util/string/deparam', function() {
propCount = 0;
delete data.route;
// If we have a route name in our `can.route` data, use it.
if ( ! ( routeName && (route = can.route.routes[routeName]))){
each(data, function(){propCount++});
// Otherwise find route.
each(can.route.routes, function(temp, name){
// best route is the first with all defaults matching
matchCount = matchesData(temp, data);
if ( matchCount > matches ) {
route = temp;
matches = matchCount
}
if(matchCount >= propCount){
return false;
}
});
each(data, function(){propCount++});
// Otherwise find route.
each(can.route.routes, function(temp, name){
// best route is the first with all defaults matching
matchCount = matchesData(temp, data);
if ( matchCount > matches ) {
route = temp;
matches = matchCount
}
if(matchCount >= propCount){
return false;
}
});
// If we have a route name in our `can.route` data, and it's
// just as good as what currently matches, use that
if (can.route.routes[routeName] && matchesData(can.route.routes[routeName], data ) === matches) {
route = can.route.routes[routeName];
}
// If this is match...
if ( route ) {
var cpy = extend({}, data),
@@ -416,7 +418,6 @@ steal('can/observe', 'can/util/string/deparam', function() {
clearTimeout( timer );
timer = setTimeout(function() {
var serialized = can.route.data.serialize();
delete serialized.route;
location.hash = "#!" + can.route.param(serialized)
}, 1);
});
View
@@ -301,6 +301,27 @@ test("updating the hash", function(){
can.$("#qunit-test-area")[0].appendChild(iframe);
});
test("sticky enough routes", function(){
stop();
window.routeTestReady = function(iCanRoute, loc){
iCanRoute("active");
iCanRoute("");
loc.hash = "#!active"
setTimeout(function(){
var after = loc.href.substr(loc.href.indexOf("#"));
equal(after,"#!active");
start();
can.remove(can.$(iframe))
},30);
}
var iframe = document.createElement('iframe');
iframe.src = steal.root.join("can/route/testing.html");
can.$("#qunit-test-area")[0].appendChild(iframe);
});
test("unsticky routes", function(){
stop();
window.routeTestReady = function(iCanRoute, loc){

0 comments on commit 863f37c

Please sign in to comment.