backbone.js とアンダースコア テンプレート システムを使用して、json ファイルからデータを動的に表示しています。<%console.log('template test') %> を使用して HTML にコンソール ログを記録すると、テンプレートが機能しません また、jsonファイルをループして「価格」を取得し、テンプレートで表示するにはどうすればよいですか?
質問が明確でない場合は、さらに説明しようとします。どんな助けでも大歓迎です、ありがとう!
このようなjsonファイルのサンプル
//addToCartResponce.json//
{
"response":{
"headers":{
"store":"123",
"id":"321"
},
"cart":{
"message":"na",
"orderId":"333",
"orderItems":{
"orderItem":[
{
"price":"$1.00",
"qty":"1",
"methods":{
"selectedMethod":{
"SelectedFFMCenter":"DDC",
"SelectedStore":"na"
}
}
}
]
}
}
}
}
これが私のバックボーンファイルです。
コレクション
var cartCollection = Backbone.Collection.extend({
el: this,
url: function() {
return window.location.href + '/assets/js/ajax/addToCartResponce.json'
},
parse: function(resp) {
return resp.response;
},
initialize: function() {
this.fetch({
async: false
});
this.on('add',this.cartFunction());
var shoppingCart = this.pluck('cart');
console.log("this.pluck('cart')");
console.log(shoppingCart);
}, // end of initialize
cartFunction: function(){
console.log('cart Functon');
}
}); // The console logs in cartCollection are working.
意見
cartContent = Backbone.View.extend({
el: $('.cartContent'),
initialize: function() {
this.render();
},
render: function( ){
this.collection = new cartCollection();
var cart_template = _.template( $(".cartContentTemplate").html() );
this.$el.html( cart_template );
return this;
}
});
cartView = new cartContent();
HTML
<script type="text/template" class ="cartContentTemplate">
<div class="cartContent">
<% console.log('template test'); %>
</div>
</script>