3
votes

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'

How do I map keys to a model?

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

UPDATE:

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?

1

1 Answers

2
votes

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

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.

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