Skip to content

Comments

Add routes in order of depth in 'on' method#39

Merged
krasimir merged 4 commits intokrasimir:masterfrom
abradley2:ordered-routes
Sep 13, 2016
Merged

Add routes in order of depth in 'on' method#39
krasimir merged 4 commits intokrasimir:masterfrom
abradley2:ordered-routes

Conversation

@abradley2
Copy link
Contributor

This change means routes with deeper urls will be given priority.

From the README:

router
  .on({
    'products/:id': function () {
      setContent('Products');
    },
    'products': function () {
      setContent('About');
    },
    '*': function () {
      setContent('Home')
    }
  })
  .resolve();

It is important to add products/:id first because otherwise you may fall into products every time

This will no longer be the case. If the 'products' route is added before 'products/:id', resolving to 'products/42' will match the 'products/:id' route. When using on routes will then be added in the order of depth (number of segments in the route).

This also prevents a possible issue from looping through the 'routes' object in the 'on' method- because ECMAScript specification does not guarantee that the keys of Objects will be in order given a for-let loop.

@krasimir krasimir merged commit de50192 into krasimir:master Sep 13, 2016
@krasimir
Copy link
Owner

Thank you for the contribution. 2.3.0 released containing your fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants