1
真的很基本的問題,但我似乎無法弄清楚爲什麼我無法將方法綁定到集合上的重置事件。綁定功能骨幹收集重置
HealthcheckList = Backbone.Collection.extend({
model: Healthcheck,
url: "/corsettiServices/rest/healthcheck/sort",
initialize: function() {
_.bindAll(this, "fetchSuccess", "addAll");
console.log("Initializing view");
this.on('reset', addAll);
this.fetch({data: {type:'all'}, processData:true, success: this.fetchSuccess, reset: true});
},
addAll: function(m, options) {
alert("adding");
},
fetchSuccess: function(m, response) {
alert("success");
console.log(m);
m.each(function(h) {
if (h.get("start")!=null) {
$('#statusTable > tbody:last').append('<tr><td>' + h.get("start") + '</td><td>' + h.get("type") + '</td><td>' + h.get("status") + '</td></tr>');
}
})
}
})
這會引發一個錯誤:Uncaught ReferenceError:addAll沒有定義。刪除「重置」事件的綁定會使數據獲取和代碼正常工作,但是我希望每次重置集合時都要執行更多處理。我錯過了如何在骨幹集合中定義函數?
自從他做了'_.bindAll'之後,甚至不需要傳遞上下文,只需要使用'this.addAll'而不是'addAll'。 – Ford
正確,並在使用上下文||這個。所以,它會默認這個。我更新了答案並提供瞭解釋。 –
謝謝,我以爲我嘗試過,但顯然不是。欣賞功能範圍的細節。 – Chris