Q:
Should I change my href attributes to be relative or should I define routes with leading slashes?
(Why do the Backbone docs suggest no leading slashes?)
Background:
The Backbone docs clearly suggest the following:
Note that you’ll want to avoid using a leading slash in your route definitions…
Now, I have these routes defined for a test:
var Router = Backbone.Router.extend({
routes: {
"index/": "logMe",
"/clients/": "logMe"
},
logMe: function (page) {
console.log(page);
}
});
Calling router.navigate($(this).attr('href'), {trigger: true}); on click, the route is captured fine for /clients/ but not for index. (Adding a leading / to the index route fixes this).
The href="…" strings I’m passing are all starting from the domain root (href="/index/" & href="/clients/").
The “href” value passed in is matched to the route property names. If your “href” values start with a slash, then your route properties have to as well.
Note that in all the examples in the Backbone docs, the fragment values do not start with a slash.