У меня сложная маршрутизация здесь. построение URL-адресов для подсмотров для навигации по различным родительским представлениям.
domain.com/categories/1/details
domain.com/categories/1/price
domain.com/categories/1/owner
domain.com/categories/2/details
domain.com/categories/2/price
domain.com/categories/2/owner
Мне нужно создать URL-адреса для получения подробностей, цен и просмотров.
<a href="#/categories/id/price">Price</a>
<a href="#/categories/id/details">details</a>
<a href="#/categories/id/owner">owner</a>
Нужно заменить динамически!
Как я могу их построить?
Вы можете попробовать следующее:
var myRouter = Backbone.Router.extend({
routes : {}
});
myRouter.on('route:categories/:id/details' function() {
});
myRouter.on('route:categories/:id/price' function() {
});
myRouter.on('route:categories/:id/owner' function() {
});
В вашем шаблоне просмотра у вас будет:
<script type="text/template" id="my-template">
<a href="#/categories/<%= id %>/price">Price</a>
<a href="#/categories/<%= id %>/details">details</a>
<a href="#/categories/<%= id %>/owner">owner</a>
</script>
Затем в вашем методе рендеринга просмотра вам просто нужно передать переменную id.
<script type="text/javascript">
var userId = 42;
var MyView = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
// Compile the template using underscore
var template = _.template( $("#my-template").html(), {id: userId});
// Load the compiled HTML into the Backbone "el"
this.$el.html( template );
}
});
</script>
Вот немного больше о том, что могло бы помочь: http://backbonetutorials.com/what-is-a-view/