Skip to content

Commit 863f37c

Browse files
committed
fixes #36 uses current route wins if all else equal
1 parent 58bfb2e commit 863f37c

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

route/route.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,27 @@ steal('can/observe', 'can/util/string/deparam', function() {
118118
propCount = 0;
119119

120120
delete data.route;
121-
// If we have a route name in our `can.route` data, use it.
122-
if ( ! ( routeName && (route = can.route.routes[routeName]))){
123-
each(data, function(){propCount++});
124-
// Otherwise find route.
125-
each(can.route.routes, function(temp, name){
126-
// best route is the first with all defaults matching
127-
128-
129-
matchCount = matchesData(temp, data);
130-
if ( matchCount > matches ) {
131-
route = temp;
132-
matches = matchCount
133-
}
134-
if(matchCount >= propCount){
135-
return false;
136-
}
137-
});
121+
122+
each(data, function(){propCount++});
123+
// Otherwise find route.
124+
each(can.route.routes, function(temp, name){
125+
// best route is the first with all defaults matching
126+
127+
128+
matchCount = matchesData(temp, data);
129+
if ( matchCount > matches ) {
130+
route = temp;
131+
matches = matchCount
132+
}
133+
if(matchCount >= propCount){
134+
return false;
135+
}
136+
});
137+
// If we have a route name in our `can.route` data, and it's
138+
// just as good as what currently matches, use that
139+
if (can.route.routes[routeName] && matchesData(can.route.routes[routeName], data ) === matches) {
140+
route = can.route.routes[routeName];
138141
}
139-
140142
// If this is match...
141143
if ( route ) {
142144
var cpy = extend({}, data),
@@ -416,7 +418,6 @@ steal('can/observe', 'can/util/string/deparam', function() {
416418
clearTimeout( timer );
417419
timer = setTimeout(function() {
418420
var serialized = can.route.data.serialize();
419-
delete serialized.route;
420421
location.hash = "#!" + can.route.param(serialized)
421422
}, 1);
422423
});

route/route_test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,27 @@ test("updating the hash", function(){
301301
can.$("#qunit-test-area")[0].appendChild(iframe);
302302
});
303303

304+
test("sticky enough routes", function(){
305+
stop();
306+
window.routeTestReady = function(iCanRoute, loc){
307+
iCanRoute("active");
308+
iCanRoute("");
309+
loc.hash = "#!active"
310+
311+
setTimeout(function(){
312+
var after = loc.href.substr(loc.href.indexOf("#"));
313+
equal(after,"#!active");
314+
start();
315+
316+
can.remove(can.$(iframe))
317+
318+
},30);
319+
}
320+
var iframe = document.createElement('iframe');
321+
iframe.src = steal.root.join("can/route/testing.html");
322+
can.$("#qunit-test-area")[0].appendChild(iframe);
323+
});
324+
304325
test("unsticky routes", function(){
305326
stop();
306327
window.routeTestReady = function(iCanRoute, loc){

0 commit comments

Comments
 (0)