ember.js - Why is a "ManyToOne" relation treated differently when serializing in ember-data? -


my question highly related "ember data: saving relationships" don't care embedding - want working onetomany (bi-directional) relationship. take example following models:

app.child = ds.model.extend({     name: ds.attr('string'),     toys: ds.hasmany('toy'), }); app.toy = ds.model.extend({     name: ds.attr('string'),     child: ds.belongsto('child') }); 

and following object creations/saves:

var store = this.get('store');  store.createrecord('child', {name: 'herbert'}).save().then(child => {     return store.createrecord('toy', {name: 'kazoo', child: child}).save()) }).then(toy => {     child.get('toys').pushobject(toy);     return child.save(); }); 

i expect child, when serialized, reference toy. e.g. like

{    'name': 'herbert',    'toys': [ 1 ] } 

but doesn't. because "manytoone" relation ship , ember-data won't serialize these: https://github.com/emberjs/data/blob/v1.0.0-beta.18/packages/ember-data/lib/serializers/json-serializer.js#l656

if make manytonone relation removing belongsto work lose reference.

why special behaviour? why manytoone different manytonne or manytomany deserves such special treatment?

where behaviour documented? totally missed , assumed bug in serializer / adapter i'm using.

what correct way achieve desired serialization?

i ended creating own, trivially modified serializer:

import emberpouch 'ember-pouch';  export default emberpouch.serializer.extend({   serializehasmany: function(snapshot, json, relationship) {     // make sure hasmany relationships serialized.     // http://stackoverflow.com/questions/20714858/ember-data-saving-relationships     var key = relationship.key;      if (this._canserialize(key)) {       var payloadkey;        // if provided, use mapping provided `attrs` in       // serializer       payloadkey = this._getmappedkey(key);       if (payloadkey === key && this.keyforrelationship) {         payloadkey = this.keyforrelationship(key, "hasmany", "serialize");       }        var relationshiptype = snapshot.type.determinerelationshiptype(relationship);        if (relationshiptype === 'manytonone' || relationshiptype === 'manytomany' || relationshiptype === "manytoone") {         json[payloadkey] = snapshot.hasmany(key, { ids: true });         // todo support polymorphic manytonone , manytomany relationships       }     }   }, }); 

the main difference being accepts manytoone relations,

and use in application/adapter.js:

export default emberpouch.adapter.extend({   defaultserializer: "pouchserial", .... 

in specific (pouch) case better store reference parent on child parent doesn't need updates when children added (which avoids conflicts).


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -