我的backbone.js代碼有什麼問題?
當一個產品頁面加載僅顯示模式的默認值...
雖然在控制檯我看到正確的數據...
這裏是我的模型:模型視圖只顯示默認值
var app = app || {};
app.Product = Backbone.Model.extend({
defaults: {
coverImage: 'img/placeholder.png',
id: null,
name: 'Unknown',
price: '100'
}
});
收藏:
var app = app || {};
app.ProductList = Backbone.Collection.extend({
model: app.Product,
url: 'php/listProducts.php'
});
還有就是我的觀點:
var app = app || {};
app.ProductItemView = Backbone.View.extend({
template: _.template($('#productPage').html()),
initialize: function(options) {
var id = options.id;
this.model = new app.Product();
this.collection = new app.ProductList();
this.collection.fetch({
success: function(collection){
console.log(collection.toJSON());
console.log('Collection models: ', collection.models[id-1]);
this.model = collection.models[id-1];
console.log('This model: ', this.model);
}
});
this.render();
},
render: function() {
this.$el.html(this.template(this.model.attributes));
return this;
}
});
路由器:
var app = app || {};
app.Router = Backbone.Router.extend({
routes: {
"product/:id": "productPageShow"
},
initialize: function() {
this.$content = $("#product-list");
},
productPageShow: function(id) {
app.prodItView = new app.ProductItemView({ id: id });
this.$content.html(app.prodItView.el);
}
});
$(function() {
new app.Router();
Backbone.history.start();
});
我的模板:
<div class="container">
<div id="product-list"></div>
</div>
<script id="productPage" type="text/template">
<div>
<img src="<%= coverImage %>" class="img-responsive" style="width:100%" alt="<%= name %>">
</div>
<div>
<h2><%= name %></h2>
</div>
<div>
<h3><%= price %></h3>
</div>
</script>
控制檯圖像:
可能的重複[Backbone:從服務器獲取的數據不顯示在視圖上](http://stackoverflow.com/questions/15330013/backbone-fetched-data-from-server-not-在視圖中顯示) –