0

コレクションへのビュー バインドを定義しました。

1) ビューを初期化すると、コレクションのフェッチ呼び出しが実行されます。
2) get リクエストの結果を見ると
問題ありません。 3) render 関数を呼び出すために (myCollection で) この変更をトリガーしたいのですが、機能しません。

ここに私のコードの一部があります:

define([
    "MyCollection",
    "!text/template"
], function (myCollection, myTemplate) {

    var myView = Backbone.View.extend({

        initialize: function ()
        {
            myCollection.bind('change', this.render); // why the render is not called when 
                                                      // I perform the fetch even if the data is correctly loaded? 

            myCollection.fetch(); // it works
        },

        render: function ()
        {
            // some code
            this.$el <-- undefined 
        }

    });

    return myView;
});

私が使用する場合

1)

   initialize: function ()
   {
       myCollection.bind('reset', this.render);  // this.$el <-- undefined in render call, Why?

        myCollection.fetch(); // it works
   }

2)

myCollection.fetch({
    success: function () {
         that.render(); // <-- here the $el is defined and it works, Why myCollection.bind('reset', this.render); not?? 
         // it seems to me that when I use reset the render function is called before the this.$el is defined
    }
});
4

2 に答える 2

1

バックボーンは、 fetch()が正常に完了したときに、変更イベントではなく、「リセット」イベントをトリガーします。

var myView = Backbone.View.extend({

    initialize: function ()
    {
        myCollection.bind('reset', this.render);
        myCollection.fetch(); // it works
    },

    render: function ()
    {
        // some code
    }

});
于 2012-05-15T21:58:54.900 に答える
1

resetイベントではなく、イベントを聞く必要がありchangeます。

それで:

myCollection.bind('reset', this.render, this);
于 2012-05-15T21:56:07.177 に答える