以下のコードでは、要素コントローラー パターンを使用して製品のコレクションをレンダリングしています。メイン ビュー テンプレートは正しくレンダリングされています。すべての要素と div セレクター「#cart-wrapper」を確認できます。どうやらメインビューが「addOne」でネストされたビューを呼び出すと、上記のセレクターを見つけることができません:
directory.CartView = Backbone.View.extend({
initialize: function(options) {
this.collection = directory.shellView.cartcollection;
},
render: function(){
this.$el.html(this.template());
this.addAll();
return this;
},
addAll: function() {
this.collection.each(this.addOne, this);
},
addOne: function(model) {
directory.cartItemView = new directory.CartItemView({model: model});
directory.cartItemView.render();
$("#cart-wrapper").append(directory.cartItemView.el);
}
});
ネストされたビュー
directory.CartItemView = Backbone.View.extend({
render: function(){
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
addOne 関数内では、 $("#cart-wrapper").length==0 . console.log(directory.cartItemView.el) を実行しましたが、テーブル内にレンダリングされたすべてのモデルでアンダースコア テンプレートは問題ないようです。
メインビューは次のように呼び出されます:
directory.cartView = new directory.CartView();
directory.cartView.render();
$("#content").html(directory.cartView.el);