如果您通過設置新集合來重置整個關係,Backbone-relational將(現在)只替換整個集合,而不是檢查差異。所以它會爲所有當前的subModels
發起remove
事件,然後爲每個新事件觸發add
事件。
我得到change
事件雖然與下面的代碼(如果貼的代碼包含一個完整的例子,雖然它會幫助;)
var SubModel = Backbone.RelationalModel.extend({});
var TheModel = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'subModels',
relatedModel: SubModel,
reverseRelation: {
key: 'TheModel'
}
}]
});
themodel = new TheModel({subModels: [{ id: 5 },{id: 7},{id: 8}]})
themodel.bind('change', function() {
console.debug('change; args=%o', arguments);
});
themodel.bind('change:subModels', function() {
console.debug('change:subModels; args=%o', arguments);
});
themodel.bind('update:subModels', function() {
console.debug('update:subModels; args=%o', arguments);
});
themodel.bind('add:subModels', function() {
console.debug('add:subModels; args=%o', arguments);
});
themodel.bind('remove:subModels', function() {
console.debug('remove:subModels; args=%o', arguments);
});
console.debug('set new subModels');
themodel.set({subModels: [{ id: 5 },{id: 7},{id: 9}] })
我們得到以下的輸出:
set new subModels
change:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, [Object { id=5}, Object { id=7}, Object { id=9}], Object {}]
change; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, undefined]
remove:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object { _related={...}}]
remove:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object { _related={...}}]
remove:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object { _related={...}}]
add:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object {}]
add:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object {}]
add:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object {}]
update:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object {}]
如果你沒有看到這些變化事件,你能檢查你使用的骨幹和骨幹關係的哪個版本?
所以我想我發現骨幹關係的createModels功能不會更新嵌套模型,因爲它們會在父模塊的更多屬性集上進行更新。它只是破壞它們並增加新的。所以這種情況發生的原因是因爲只有添加/刪除事件纔會引發更改事件。這也是爲什麼所有這些事件都會在數據相同時觸發的原因。至少這是我現在的想法,讓我知道這是對還是錯。謝謝! – fancy