mercredi 21 octobre 2015

how to set type and id in normalizeArrayResponse

I am building an Ember 2.1.0 app on a server where I cannot control format of the json response, which, in this case is just an array of objects without a top level namespace. Each of the objects has an id and a type (in this case post), as well as other properties, but there is no top level.

[
  Object: id: "1", type: "post", content: "blah",
  Object: id: "4", type: "post", content: "foo"
]

Therefore, to build an Ember app on it, I have to (in Ember 2.1.0) use normalizeArrayResponse (the old extractArray in pre Ember 2.0 app).

I am doing this

 normalizeArrayResponsee: function(store, type, payload){
        var convertedPayload = {};
        convertedPayload = {};
        convertedPayload[type.modelName] = payload;
        return this._super(store, type, convertedPayload)
    }

I am getting this error

Error while processing route: posts Assertion failed: You must include an 'id for undefined in an object passed to 'push' Error: Assertion Failed`

Inspecting the push function in the console I see this

line `1570  var modelName  data.type; modelName = "post", data = Object{id: "4", type: "post"...}, type = undefined

In light of this (type = undefined), I added the following line to my normalizeArrayResponse

convertedPayload.type = type;

but that didn't fix the problem and also gave me a warning

Encountered "type" in payload, but no model was found for model name "type" (resolved model name using em@serializer:posts.modelNameFromPayloadKey("type")




Aucun commentaire:

Enregistrer un commentaire