我會盡力詳細解釋我的問題,但如果缺少什麼東西,請戳我,我會很樂意給你額外的信息..骨幹火災'添加'事件後'排序'骨幹。關係收集
我使用主幹0.9.10,主幹關係0.7.1,下劃線1.4.4。
我設置一個支柱關係集合這樣的:
..和創建了動態重視聽衆一個傳遞的集合骨幹觀點:
// ActorDisplay.js
Backbone.View.extend({
attachListeners: function (collection) {
this.listenTo(collection, 'add', _.bind(this.onAdd, this));
this.listenTo(collection, 'sort', _.bind(this.onCollectionSort, this));
},
onAdd: function() {
console.log('onAdd fired');
},
onCollectionSort: function() {
console.log('onCollectionSortFired');
}
});
現在,這裏來了好的部分:
我使用關係的.find或者創建方法來創建一個新的ActorAssignment模型:
// AddAssignmentCommand:
execute: function (data) {
var sortIndex = data.collection.length,
actorAssignment = data.collection.model.findOrCreate({
actor: data.actor,
sort_index: sortIndex,
process_model: data.processModel
});
data.collection.add(actorAssignment);
}
之前,與骨幹0.9.2的「排序」事件總是在「添加」事件後被解僱,但現在不是了。我花了幾個小時試圖調試這個,這裏是我的結果..似乎模型上'添加'傳播的集合被延遲,而'sort'直接觸發集合,這會導致我的應用程序出現奇怪的行爲。
我已經把一些調試語句中的骨幹源代碼,這是輸出:
triggering add events.. 6dd47c9_backbone_1.js:641
triggering add event on model 6dd47c9_backbone_1.js:644
trigger: add fired 6dd47c9_backbone_1.js:182
triggering sort events.. 6dd47c9_backbone_1.js:647
trigger: sort fired 6dd47c9_backbone_1.js:182
*onCollectionSortFired* ControlStructureActorDisplay.js:136
trigger: relational:add fired 6dd47c9_backbone_1.js:182
trigger: change:entity_type fired 6dd47c9_backbone_1.js:182
trigger: change:entity_type fired 6dd47c9_backbone_1.js:182
trigger: change:entity_type fired 6dd47c9_backbone_1.js:182
trigger: change:entity_type fired 6dd47c9_backbone_1.js:182
trigger: change:actor fired 6dd47c9_backbone_1.js:182
trigger: change:actor fired 6dd47c9_backbone_1.js:182
trigger: change:actor fired 6dd47c9_backbone_1.js:182
trigger: change:actor fired 6dd47c9_backbone_1.js:182
trigger: change:process_step fired 6dd47c9_backbone_1.js:182
trigger: change:process_step fired 6dd47c9_backbone_1.js:182
trigger: change:process_step fired 6dd47c9_backbone_1.js:182
trigger: change:process_step fired 6dd47c9_backbone_1.js:182
trigger: change:sort_index fired 6dd47c9_backbone_1.js:182
trigger: change:sort_index fired 6dd47c9_backbone_1.js:182
trigger: change:sort_index fired 6dd47c9_backbone_1.js:182
trigger: change:sort_index fired 6dd47c9_backbone_1.js:182
trigger: change fired 6dd47c9_backbone_1.js:182
trigger: change fired 6dd47c9_backbone_1.js:182
trigger: change fired 6dd47c9_backbone_1.js:182
trigger: change fired 6dd47c9_backbone_1.js:182
trigger: add fired 6dd47c9_backbone_1.js:182
trigger: add fired 6dd47c9_backbone_1.js:182
trigger: add:actor_assignments fired 6dd47c9_backbone_1.js:182
trigger: add:actor_assignments fired 6dd47c9_backbone_1.js:182
trigger: add fired 6dd47c9_backbone_1.js:182
*onAdd fired* ControlStructureActorDisplay.js:155
正如你所看到的,使用onAdd是onCollectionSort後被解僱的方式,而「排序」事件在骨幹collection.add中的'add'事件之後觸發,在644行左右。上面的意圖是什麼?或者它是一個錯誤?另外,我不知道這是一個特定的關係問題,還是Backbone本身有問題。最後,我的問題提出了:如果事件不是像上面例子那樣阻塞,還是應該在假設事件按特定順序被觸發時更加謹慎?
編輯:我添加了process_model屬性的findOrCreate通話,因爲這引起了我的問題,答案在下面。