1

このビューをコレクション変更イベントにバインドして、新しいアイテムがコレクションに追加されたときにリセットされるようにするにはどうすればよいですか?

KAC.Views.ModuleMainNavigation = Backbone.View.extend(
    {
        tagName: "div",
        id: "",
        className: "",
        template: JST['modules/main_navigation'],
        initialize: function() {
            _.bindAll(this);
        },
        events: {
        },
        render: function () {
            this.$el.html(
                this.template(
                    { 
                        collection : this.collection 
                    }
                )
            );

            return this;
        }
    }
);
4

1 に答える 1

3

change イベントをリッスンする必要があります。
ほとんどの場合、これはinitialize関数で行われます。

すべてのイベント (モデルの変更、コレクションのリセット、新しいモデル、モデルの削除) をリッスンできます。

this.collection.on('change reset add remove', this.render, this);

または、新しいモデルが追加されたイベントのみ:

this.collection.on('add', this.render, this);

backbone.js コレクション イベントも参照してください。

于 2013-08-17T08:38:56.843 に答える