jeudi 17 décembre 2015

How to normalize returned sideload payload of model.save in ember-data

I try to save and return the data of related models with one statement by a defined serializer for a model. While ember-data's default save payload (to and from server) would look like this:

{
    "posten":{
        "id":4711,
        "name":"Mein Posten"
    }
}

I built a serializer like this:

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

So the data to the server looks like this:

{
    "posten": {
        "postenId": 3051,
        "name": "Mein Posten",
    },
    "postenaufteilung": [
    {
        "postenaufteilungid": 3051,
        "postenId": 3051,
        "kontodebitorid": 1002,
        "anteil": 0.26,

    }
  ]
}

To this this point, everything is working as expected. The problem occurs when I try to return the same data from the server, I get this error:

Cannot convert object to primitive value at SETTER_FUNCTION [as text]...

The code to save the model looks like this:

postenListSave.forEach(function (item) {
    if (item.get('hasDirtyAttributes')) {
        deferreds.push(item.save());                    
    }                
});
Ember.RSVP.all(deferreds).then(function () {
    if (!Em.isNone(callback)) {
        callback();
    }
    _self.set("isSaving", false);
}, function (e) {
    console.log(e);
    _self.set("isSaving", false);
});

I have tried to push the payload into the store by myself by using the serializers "normalizeSaveResponse" - method. But when I try to push data like this:

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

Ember reports an error that there are no ids in the posten.postenaufteilung - objects.




Aucun commentaire:

Enregistrer un commentaire