1

以下のコードでは、要素コントローラー パターンを使用して製品のコレクションをレンダリングしています。メイン ビュー テンプレートは正しくレンダリングされています。すべての要素と 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);
4

1 に答える 1

1

これは$("#cart-wrapper")、ビューのルート要素をページに追加する前に呼び出すためであり、jQueryそれが探している場所です。それを修正するには、this.$("#cart-wrapper")代わりに呼び出してください。

于 2013-09-15T11:16:22.010 に答える