Permalink
Browse files

Improve the sorting algorithm for routing

If one route is an explicit substring of another,
prefer the longer one.

Otherwise, if one of the routes has a dynamic
segment, prefer the route without the dynamic
segment.

Otherwise, prefer the longer one.

This fixes posts/:post_id being preferred over
posts/new.

Eventually, we should prebuild the match tree.
  • Loading branch information...
wycats committed Jun 14, 2012
1 parent be69395 commit 2d26a36f4119ffa0f8b41157dbb2856f27a33420
Showing with 15 additions and 0 deletions.
  1. +15 −0 packages/ember-routing/lib/routable.js
@@ -260,6 +260,21 @@ Ember.Routable = Ember.Mixin.create({
var childStates = get(this, 'childStates'), match;
childStates = childStates.sort(function(a, b) {
var aDynamicSegments = getPath(a, 'routeMatcher.identifiers.length'),
bDynamicSegments = getPath(b, 'routeMatcher.identifiers.length'),
aRoute = get(a, 'route'),
bRoute = get(b, 'route');
if (aRoute.indexOf(bRoute) === 0) {
return -1;
} else if (bRoute.indexOf(aRoute) === 0) {
return 1;
}
if (aDynamicSegments !== bDynamicSegments) {
return aDynamicSegments - bDynamicSegments;
}
return getPath(b, 'route.length') - getPath(a, 'route.length');
});

0 comments on commit 2d26a36

Please sign in to comment.