如何构造用于骨干路由的动态url
how to construct dynamic urls for backbone routing?
这里有复杂的路由。为子视图构建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>
需要动态替换id!
我该如何构建它们?
你可以试试这个:
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/
相关文章:
- 以角度管理动态URL
- javascript在动态Url中匹配并添加字符串
- 在angular.js中定义动态URL的重定向
- 对使用 Ajax 捕获的动态 url 响应
- jQuery UI中的动态url,数据表
- 限制与AngularJS状态匹配的动态URL参数的最佳方式
- URL重写Node js-动态URL
- 如何构造用于骨干路由的动态url
- Facebook选项卡应用程序动态URL
- 使用ui路由器实现动态URL路由的最佳方式是什么
- 如何从json文件中的基本url生成动态url
- 如何从JSON文件中读取动态url
- 带有标签的动态url的社交共享
- 基于表单输入生成动态 URL
- 另一种在javascript中创建随机动态url的方法/阻止wpengine缓存
- 从用户输入到搜索的动态 URL
- SAPUI5 和 OData 服务的动态 URL
- 使用动态 URL 初始化时保存主干模型
- 动态 url 作为 css 和 D3JS 中伪元素的数据属性
- 使用Javascript/Jquery来删除动态URL