Backbone relational events not firing?
Asked Answered
C

1

6
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!!!!

Cholon answered 7/10, 2011 at 10:7 Comment(1)
So I think I figured out that backbone-relational's createModels functionality won't update nested models upon further sets of attributes to the parent module. It just clobbers them and adds new ones. So the reason this was happening was because only add/remove events were firing not change events. It's also why all those events fire when the data is the same. At least this is my thinking right now let me know if this is right or wrong. Thanks!Cholon
F
0

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?

Flush answered 2/11, 2011 at 11:3 Comment(1)
I have the same problem on Backbone 0.5.3 and Backbone-Relational 0.4.0--Add and Remove events fire on parent relations. Changes do nothing.Cantwell

© 2022 - 2024 — McMap. All rights reserved.