Ember Data - 适配器没有方法'map'

问题描述:

Ember数据对JSON响应颇为挑剔。我的服务器响应中有一些不规则键,根据指南,我可以将它们映射到适配器上。
http://emberjs.com/guides/ model / the-rest-adapter /#toc_underscored-attribute-names
但是,Ember说:类型错误:Object没有方法'map'

Ember Data is picky about JSON responses. I have some 'irregular keys' in the response from my server, and according to the guide I can map those onto the adapter. http://emberjs.com/guides/models/the-rest-adapter/#toc_underscored-attribute-names However, Ember says: Type Error: Object has no method 'map'

如何将键映射到模型?

我正在使用Ember v1.0.0和Ember Data v1 .0.0-beta.1

I am using Ember v1.0.0 and 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' }
});

更新:

数据文件,似乎我可以像这样添加地图功能到适配器:

Looking at the Ember Data docs, it seems I can add the map functionality to the adapter like this:

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];
    }
  })
});

我尝试过这个,但地图似乎没有工作。
没有更多错误,但映射的属性为空。为什么?

I tried this, but map doesn't seem to be working. There are no more errors, but the mapped attribute(s) are null. Why?


我试过这个,但是地图似乎没有工作。没有更多的错误,但映射属性为空。为什么?

I tried this, but map doesn't seem to be working. There are no more errors, but the mapped attribute(s) are null. Why?

似乎你引用的指南是过时的,RESTAdapter不再有一个地图功能。将fx添加到您的适配器中,摆脱了运行时错误,但这些错误只是更大问题的症状,即从RESTAdapter中删除了映射属性功能。

It seems like the guide you referenced is out-of-date, RESTAdapter no longer has a map function. Adding that fx to your adapter got rid of the runtime errors but those were just symptom of a larger issue, that the mapping attributes feature has been removed from RESTAdapter.

在ember-data beta中,json数据现在通过自定义适配器中的几个钩子中的一个来进行归一化。请参阅 rest-adapter-and-serializer-一些例子的配置

In ember-data beta, json data is now normalized by customizing one of several hooks in the adapter. See rest-adapter-and-serializer-configuration for some examples