Dan*_*ley 2 javascript backbone.js
这是我的问题
我有一个非常简单的骨干集合为我获取一些数据.一切正常如下:
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');
}
});
Run Code Online (Sandbox Code Playgroud)
当fetch被调用时,按预期将解析日志记录到控制台.
但在那之后我想听取重置事件,这样我就可以使用该集合来填充bootstrap 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');
}
});
Run Code Online (Sandbox Code Playgroud)
现在,解析事件永远不会被触发,集合也不会填充,我无法弄清楚原因.
任何见解都非常感谢,谢谢.
您没有reset使用该代码与事件挂钩.您将覆盖默认的Backbone.Collection reset方法(您不希望这样做).
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
}
});
Run Code Online (Sandbox Code Playgroud)
我认为你在_.bindAll收听Backbone事件时感到困惑.bindAll做了不同的事情,你不需要它.
| 归档时间: |
|
| 查看次数: |
794 次 |
| 最近记录: |