lundi 13 avril 2015

Serialize JSON into Ember-Data (two different models in payload)

I have JSON coming from the server which looks like:



data: {
user: {
address: {
id: "id",
city: "city",
street: "street",
.......
}
name: "name",
......
}

authentication: {
token: "token",
id: "id"
}
}


In Ember I have a model auth which should represent the authentication model that I get from the server. Since I have different names for same model on the server and in the Ember store, I wrote a serializer with one method typeForRoot(root) where I just map server authentication to Ember auth. Problem is that this typeForRoot is actually never invoked, and I do not have a slightly idea why.


Here is Ember auth model:



import DS from 'ember-data';

export default DS.Model.extend({
token: DS.attr('string')

});


And here is my serializer:



import DS from 'ember-data';
import Ember from 'ember';

export default DS.RESTSerializer.extend({

typeForRoot: function(root) {
// 'authentication' should become 'auth'
var subRoot = root.substring(0, root.length - 10);

// _super normalizes 'authentication' to 'auth'
return this._super(subRoot);
}
});


My user model is being properly saved in the store (I'm using a separate, model based, serializer for user), but I get the message WARNING: Encountered "authentication" in payload, but no model was found for model name "authentication" (resolved model name using shop-app@serializer:user:.typeForRoot("authentication")) when authentication model needs to be saved in auth model in the store.


Does anyone know how can I overcome this problem. Thanks, Milan





Aucun commentaire:

Enregistrer un commentaire