主干路由器不能显示视图
Backbone Router not working to display view
我花了很多时间学习Backbone的视图和模型。我现在正试图了解路由器以及如何实现它们。我不太确定为什么我的路由器不显示PriceView
视图。
我想显示点击链接时在收藏模型中找到的价格。我不知道为什么这不起作用。
下面是我的代码:http://jsfiddle.net/pHXQg/
这是我的JavaScript:
// Model
var TheModel = Backbone.Model.extend({
defaults: {
name: '',
shortDescription: '',
price: ''
}
});
// Collection
var TheCollection = Backbone.Collection.extend({
model: TheModel
});
// Instantiate a new collection and populate it with some data.
var aCollection = new TheCollection({
name: 'Mustang',
shortDescription: 'Pretty nice even if it is overpriced',
price: '9999'
});
// View for the title
var TitleView = Backbone.View.extend({
el: '.js-title',
initialize: function () {
this.collection = aCollection;
return this;
},
render: function () {
var compiledTemplate = _.template( $('#title').html(), { data: this.collection.toJSON() });
this.$el.html(compiledTemplate);
return this;
}
});
// View for the price
var PriceView = Backbone.View.extend({
el: '.js-price',
initialize: function () {
this.collection = aCollection;
return this;
},
render: function () {
var compiledTemplate = _.template( $('#price').html(), { data: this.collection.toJSON() });
this.$el.html(compiledTemplate);
return this;
}
});
// Instantiate a new TitleView
var titleView = new TitleView();
titleView.render();
// Router
var TheRouter = Backbone.Router.extend({
routes: {
'price': 'price' // #price
},
price: function () {
new PriceView();
}
});
// Instantiate a new Router
var router = new TheRouter();
// Start browser history
Backbone.history.start();
这是我的HTML和模板:
<script type="text/template" id="title">
<h1><%- data[0].name %></h1>
<a href="#price">Click here to see the price</a>
</script>
<script type="text/template" id="price">
<p><%- data[0].price %></p>
</script>
<div class="container">
<div class="row">
<div class="js-title">
</div>
</div>
<div class="row">
<div class="js-price">
</div>
</div>
</div>
路由器不会自动调用PriceView的render()函数。
一种方法是将路由器更改为:var TheRouter = Backbone.Router.extend({
routes: {
'price': 'price' // #price
},
price: function () {
new PriceView().render();
}
});
相关文章:
- [AngularJS][UI Router]在视图中显示视图
- 可以't无法显示视图/集合
- 在 Angular JS 中显示视图上的数组
- 如何在主干中显示视图.js(路由器.js中不显示警报)
- Angular 在加载其他视图之前不显示视图
- 如何将自动完成搜索框重定向到显示视图
- 使用 Knockoutjs 显示视图模型中数据的更好方法
- Angular UI路由器未显示视图
- 无法在“角度”中显示视图
- 在新窗口中显示视图
- 显示视图时JavaScripts不工作-Rails 4 Ruby 2
- 如何在单击按钮时显示视图
- AngularJS$routeProvider未显示视图
- 将创建视图嵌套在Backbone.js中的显示视图中
- 烬不显示视图
- MVC应用程序不显示视图中的数据
- 主干路由器不能显示视图
- 流星js |通过帮助器显示视图中的Json
- 如何在HTML或css中禁用选中的突出显示视图
- 我如何嵌入街景图像,使其显示视图从街道(而不是从建筑物内部)