mardi 22 décembre 2015

How to serialize embedded related data in PUT-Request in ember-data

I am sending and returning data from ember-data to my RESTService like this:

{
    "posten":{
        "id":4711,
        "name":"Mein Posten",
        "postenaufteilung":[{id:1,name...},...]        
    }
}

The serializer is configured for embedded data:

attrs: {
    postenaufteilung: {
        embedded: 'always',
        serialize: 'postenaufteilung'
    }
},

To send data to my RESTService in the correct way, I need to serialize the data:

serializeIntoHash: function (data, type, record, options) {
    //Ember.merge(data, this.serialize(record, options));        
    var root = Ember.String.decamelize(type.modelName),
    serialized = this.serialize(record, options);
    data[root] = serialized;
    data.posten["postenaufteilung"] = [];
    record.hasMany('postenaufteilung').forEach(function (postenaufteilungsitem) {
        paSerialisiert = postenaufteilungsitem.serialize();
        if (Em.isNone(paSerialisiert.posten)) {
            paSerialisiert.posten = 0;
        }
        data.posten.postenaufteilung.push(paSerialisiert);
    });
},

Everything works as expected, only when I save my model and ember-data sends a PUT, the returned embedded model (postenaufteilung) is not refreshing the model inside the store.

Am I missing something here?




Aucun commentaire:

Enregistrer un commentaire