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;
}
});
라우터 : 0
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>
콘솔 이미지 :
[백본 : 서버에서 가져온 데이터를보기에 표시하지 않음] 가능한 복제본 (http://stackoverflow.com/questions/15330013/backbone-fetched-data-from-server-not- display-on-view) –