Reputation: 222621
I am playing around with sammy.js and I have a problem with a trailing slash in the end of the url. Here is an example:
I want my app to do a particular thing when the url looks like #!users
and I can easily achieve this with the following code:
Sammy(function() {
this.get('#!users', function(){
console.log('users');
});
}).run();
The problem is that I want it to behave the same way with #!users/
. Assuming that I have a lot of such different urls I do not want to copy the code around, is there a way in sammy.js to handle in the same way url
and url/
?
Upvotes: 1
Views: 258
Reputation: 2340
I had this same problem, and decided to fix it with a catch-all at the end of my Sammy set-up. My solution removes the trailing slash if there is one:
Sammy(function () {
this.get('#', function () {
// ...
});
this.notFound = function (method, path) {
if (path[path.length - 1] === '/') {
// remove trailing slash
window.location = path.substring(0, path.length - 1);
} else {
// redirect to not found
}
}
});
Upvotes: 1
Reputation: 222621
After reading more carefully documentation I have found a hack to do this (there is no specific method to have such behavior). So my way of doing this is:
Sammy(function() {
this.get('#!users/?', function(){
console.log('Users');
});
this.get('#!users/:id/?', function(){
console.log('User with id ' + this.params.id);
});
}).run();
So, all I need is to add /?
in the end (which in regular expressions means one or 0 of /)
Upvotes: 0