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 su così themodel.get('subModels') restituisce una raccolta di modelli.Gli eventi relazionali di backbone non si attivano?


dati sottomodello Ora, se mi passa cambiato in mymodel

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

Non dovrebbero themodel gettare un evento change? Non fa per me.


Tanto più se mi passa i dati identici in mymodel

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

themodel.attributes.subModels getta add e update eventi, anche se non c'è nulla di nuovo.

Non sono sicuro del motivo per cui questi problemi stanno accadendo, qualsiasi aiuto sarebbe fantastico, grazie !!!!

+0

Quindi penso di aver capito che la funzionalità createModels di backbone-relational non aggiornerà i modelli annidati su ulteriori serie di attributi al modulo genitore. Li coglie e ne aggiunge di nuovi. Quindi la ragione per cui questo stava accadendo era perché solo gli eventi add/remove stavano sparando e non cambiano gli eventi. È anche il motivo per cui tutti quegli eventi si attivano quando i dati sono gli stessi. Almeno questo è il mio modo di pensare in questo momento fammi sapere se questo è giusto o sbagliato. Grazie! – fancy

risposta

0

Se si ripristina un'intera relazione in questo modo impostando una nuova raccolta, Backbone-relational (al momento) sostituirà semplicemente l'intera raccolta, invece di verificare le differenze. Quindi verrà generato un evento remove per tutti gli attuali subModels, quindi verrà attivato lo add eventi per ogni nuovo evento.

io capisco change eventi, però, con il seguente codice (sarebbe utile se il codice inviato contiene un esempio completo però;)

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

Questo produce il seguente 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 {}] 

Se non vedi questi eventi di cambiamento, potresti verificare quali versioni di backbone e backbone-relazionali stai usando?

+0

Ho lo stesso problema su Backbone 0.5.3 e Backbone-Relational 0.4.0 - Aggiungi e rimuovi gli eventi sugli attacchi parent. Le modifiche non fanno nulla. –