I’m using the router function in backbone.js and came across this problem. This may be a trivial thing, but I cant seem to figure this out nor find anything on Google.
Problem: On the page http://www.mydomain.com/user/1 there is a link. This link should link to http://www.mydomain.com/user/1/profile.
Of course if I use <a href="1/profile"> I get what I am looking for, but 1 is a dynamically generated value. How then should my router define the routes? I believe its not a wise choice to hardcode the number 1 into the routes.
//Router
var AppRouter = Backbone.Router.extend({
routes: {
'': 'profile',
'profile': 'profile'
},
profile: function() {
}
});
var app = new AppRouter();
Backbone.history.start();
When I set the href attribute of the a tag like <a href="profile">, the link that results is http://www.mydomain.com/user/profile.
For <a href="./profile"> I get http://www.mydomain.com/user/profile.
For <a href="/profile"> I get http://www.mydomain.com/profile.
For <a href="profile/"> I get http://www.mydomain.com/profile/.
Why is the 1 missing, and how can I keep it to achieve what I want?
You can’t define this dynamic URLs directly in the HTML, you have to create them into your Views.
For example:
template:
js code:
Or, as I do sometimes when I feel in the way:
template:
js code: