Reputation: 2469
I am working on a durandal js 2.0 application. All works fine, except that the routes that I define are case-sensitive. I can also say that the starter kit for durandal 2.0 has the same problem.
consider this as my route.
router.map([
{ route: 'flickr', moduleId: 'viewmodels/flickr', nav: true }
]);
navigating to
http://localhost/durandal#flickr
works without a problem, but navigating to
http://localhost/durandal#Flickr does not.
Is there a way for the routes to be treated as case-insensitive.
Upvotes: 1
Views: 480
Reputation: 14995
You can use mapUnknownRoutes and do a search for each route in lowercase against the default route list. This would mean all your routes would be lowercased when they are created.
http://durandaljs.com/documentation/Using-The-Router/
This would be done by using
router.mapUnknownRoutes(function (instruction) {
Ko.arrayForEach(router.instructions, function(route) {
if (route.route === instruction.fragment)
// set route to this route
});
I am on my iPad so just giving you a suggestion not saying that works as is...
Another thing you may be able to do is set the regexp on the router. Look at the API and it shows each instruction has a config property that contains a routing regular expression. Try setting that to fix your route matching.
Upvotes: 0
Reputation: 1541
How about setting up the route as an array? That way you could cater for both eventualities.
router.map([
{ route: ['flickr', 'Flickr'],
moduleId: 'viewmodels/flickr',
title: 'flickr',
nav: true }
]);
If too many possible combinations to cater for with an array, the only option would be regular expression matching.
Rob Eisenberg suggests this solution for Durandal 1.x:
router.mapNav({ name: "details", url: new RegExp('details', "i") , hash:'#/details', moduleId:'viewmodels/details'});
As a minimum, you must include the moduleId that that route maps to. If it's a navigation route and you are databinding to it's hash, then you need to manually specify the default hash as well. The reason is that we cannot autmatically derive the moduleId or the hash from a RegEx.
This would translate to:
router.map([
{ route: new RegExp('flickr', "i"),
moduleId: 'viewmodels/flickr',
title: 'flickr',
nav: true }
]);
in your case.
Upvotes: 4