我有一个博客应用程序,API调用用户“用户”,但我的Ember模型是“作者”.
@H_502_2@App.Author = DS.Model.extend({
name: DS.attr(),posts: DS.hasMany('post',{async: true}),email: DS.attr()
});
我将传入的JSON映射到AuthorSerializer:
@H_502_2@App.AuthorSerializer = DS.RESTSerializer.extend({ normalizePayload: function(type,payload) { return this.translateRootKey('user','author',payload); },translateRootKey: function(server_word,client_word,payload) { var response = {},key = payload[server_word] ? client_word : Ember.String.pluralize(client_word),value = payload[server_word] ? payload[server_word] : payload[Ember.String.pluralize(server_word)]; response[key] = value; return response; } });但是当我坚持服务器时,我无法弄清楚如何更改POST / PUT的根目录.我希望我的root是“用户”或“用户”(取决于具体情况).服务器目前从Ember获取此参数:
@H_502_2@{"author"=>{"name"=>"asdf","email"=>"asdf"},"user"=>{}}在与服务器通信时,如何告诉Ember使用“user / s”作为我的密钥名称?
例:
@H_502_2@{"user"=>{"name"=>"asdf","email"=>"asdf"}}解决方法
我想你正在寻找serializeIntoHash钩子.
@H_502_2@App.AuthorSerializer = DS.RESTSerializer.extend({
serializeIntoHash: function(hash,type,record,options) {
hash["user"] = this.serialize(record,options);
}
});