主干关系事件未触发?

发布于 2024-12-08 10:04:11 字数 935 浏览 1 评论 0原文

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') 返回模型集合。


现在,如果我将更改后的子模型数据传递到 mymodel 中,

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

themodel 不应该抛出 change 事件吗?这不适合我。


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

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

themodel.attributes.subModels 会抛出 addupdate 事件,即使没有什么是新的。

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

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}]})

I have createModels on so themodel.get('subModels') returns a collection of models.


Now if I pass changed subModel data into mymodel

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

Shouldn't themodel throw a change event? It doesn't for me.


More so if I pass identical data into mymodel

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

themodel.attributes.subModels throws add and update events, even though nothing is new.

I'm not sure why these issues are happening, any help would be great, thanks!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-12-15 10:04:11

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

如果您没有看到这些更改事件,您可以吗检查您正在使用哪个版本的主干和主干关系?

If you reset a whole relation like that by setting a new collection, Backbone-relational will (at the moment) just replace the whole collection, instead of checking for differences. So it'll fire a remove event for all current subModels, then fire add events for each new one.

I do get change events though, with the following code (it would help if posted code contains a complete example though ;)

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}] } )

This yields the following output:

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 {}]

If you don't see these change events, could you check which versions of backbone and backbone-relational you're using?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文