【问题标题】:Backbone relational events not firing?骨干关系事件未触发?
【发布时间】:2011-12-02 21:39:02
【问题描述】:
class TheModel extends Backbone.RelationalModel
    relations:[
        type: Backbone.HasMany
        key: 'subModels'
        relatedModel: SubModel
        collectionType: SubModels
        reverseRelation:
            key: 'TheModel'
    ]

themodel = new the TheModel({subModels:[{#stuff},{#stuff},{#stuff}]})

我开启了 createModels,所以 themodel.get('subModels') 返回模型的集合。


现在,如果我将更改后的 subModel 数据传递给 mymodel

themodel.set({subModels:[{changedstuff},{stuff},{stuff}]})

themodel 不应该抛出一个change 事件吗?它不适合我。


如果我将相同的数据传递给mymodel,更是如此

themodel.set({subModels:[{samestuff},{samestuff},{samestuff}]})

themodel.attributes.subModels 引发 addupdate 事件,即使没有什么新鲜事。

我不确定为什么会出现这些问题,任何帮助都会很棒,谢谢!!!!

【问题讨论】:

  • 所以我想我发现主干关系的 createModels 功能不会根据父模块的进一步属性集更新嵌套模型。它只会破坏它们并添加新的。所以发生这种情况的原因是因为只有添加/删除事件会触发而不是更改事件。这也是为什么当数据相同时所有这些事件都会触发的原因。至少这是我现在的想法,让我知道这是对还是错。谢谢!

标签: javascript model backbone.js coffeescript backbone-relational


【解决方案1】:

如果您通过设置新集合来重置整个关系,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 {}]

如果您没有看到这些更改事件,您能否检查一下您正在使用哪些版本的主干和主干关系?

【讨论】:

  • 我在 Backbone 0.5.3 和 Backbone-Relational 0.4.0 上遇到了同样的问题——添加和删除事件在父关系上触发。更改无济于事。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多