Ember Data对JSON响应很挑剔。我的服务器响应中有一些“不规则键”,根据指南我可以将它们映射到适配器上。
http://emberjs.com/guides/models/the-rest-adapter/#toc_underscored-attribute-names
但是,Ember说:Type Error: Object has no method 'map'
如何将键映射到模型?
我正在使用Ember v1.0.0和Ember Data v1.0.0-beta.1
App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'api',
host: 'http://localhost:8080'
});
App.Customer = DS.Model.extend({
name: DS.attr('string')
});
App.CustomersRoute = Ember.Route.extend({
model: function () {
var store = this.get('store');
return store.findAll('customer');
}
});
App.ApplicationAdapter.map('App.Customer', {
name: { key: 'Name' }
});
更新:
查看Ember Data文档,似乎我可以像这样添加地图功能到适配器:
DS.Adapter.reopenClass({
map: DS._Mappable.generateMapFunctionFor('attributes', function(key, newValue, map) {
var existingValue = map.get(key);
for (var prop in newValue) {
if (!newValue.hasOwnProperty(prop)) { continue; }
existingValue[prop] = newValue[prop];
}
})
});
我试过这个,但地图似乎没有用。 没有更多错误,但映射的属性为空。为什么呢?
答案 0 :(得分:2)
我试过这个,但地图似乎没有用。没有更多错误,但映射的属性为空。为什么呢?
您引用的指南似乎已过时,RESTAdapter不再具有map
功能。将fx添加到适配器可以消除运行时错误,但这些只是一个更大问题的症状,映射属性功能已从RESTAdapter中删除。
在ember-data beta中,json数据现在通过自定义适配器中的几个钩子中的一个来规范化。有关示例,请参阅rest-adapter-and-serializer-configuration