2

これが私の問題です

私はいくつかのデータを取得する非常に単純なバックボーン コレクションを持っています。次のようにすべてが正常に機能します。

DealershipContacts.Collection = Backbone.Collection.extend({
    url:jarvisUrl ("dealership_contacts"),
    parse:function(response) {
        console.log('parse called');
        return response.data;

    },
    initialize : function(){
        _.bindAll(this, 'reset', 'parse');
    }
});

fetch が呼び出されると、期待どおりに解析ログがコンソールに出力されます。

しかし、その後、リセット イベントをリッスンして、コレクションを使用してブートストラップの先行入力のソース データを設定できるようにしたいと考えています。だから私はこれをしました:

DealershipContacts.Collection = Backbone.Collection.extend({
    url:jarvisUrl ("dealership_contacts"),
    parse:function(response) {
        console.log('parse called');
        console.log(response);
        return response.data;

    },
    reset:function(){
        console.log("change fired");
        $('.dealership_typeahead').typeahead({source:this.pluck('user_name')});
        return true;
    },
    initialize : function(){
        _.bindAll(this, 'reset', 'parse');
    }
});

そして今、解析イベントは発生せず、コレクションは作成されません。理由がわかりません。

どんな洞察も大歓迎です、ありがとう。

4

1 に答える 1

5

resetそのコードでイベントに接続していません。デフォルトのBackbone.Collectionresetメソッドをオーバーライドしています(そうしたくありません)。

DealershipContacts.Collection = Backbone.Collection.extend({
    url:jarvisUrl ("dealership_contacts"),

    initialize: function(models, options){
        this.on('reset', this.doStuff, this);
    },
    parse:function(response) {
        // you DO want to override the default parse method
        return response.data;
    },
    // don't call this method `reset` :)
    doStuff:function(){
        // do something now that collection is fetched
    }
});

_.bindAllBackbone イベントのリッスンを混乱させていたと思います。bindAllは別のことを行いますが、これには必要ありません。

于 2013-03-22T04:21:04.177 に答える