2013-03-22 13 views
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調用解析日誌到控制檯按預期。

但是在此之後,我想傾聽重置事件,以便我可以使用該集合填充引導程序typeahead輸入的源數據。所以我這樣做:

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'); 
    } 
}); 

而現在的解析事件永遠不會被解僱,集合不填充我找不出原因。

任何見解非常感謝,謝謝。

回答

5

你沒有使用該代碼連接到reset事件。您正在覆蓋默認的Backbone.Collection reset method(您不想這麼做)。

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 
    } 
}); 

我覺得你用監聽骨幹事件撲朔迷離_.bindAllbindAll做了一些不同的事情,你不需要它。