4

I'm beginning to work with ember-data and I have a problem to map the data

Here an exemple of my code ( i've place a jsonTest as an exemple of the data received from de backend , I don't work on the backend and I cannot modify the response from the server )

Clive = Ember.Application.create();
// MODEL


Clive.Deadline = DS.Model.extend({
    title : DS.attr('string'),
});

jsonTest = '{"items":[{"id":93,"title":"title","date":"14-11-2012"}]}';

// Adapter 
Clive.adapter = DS.Adapter.create({
    findAll : function(store,type){
        var  self = this;        
        self.didFindAll(store, type, jsonTest);
    }
});

Clive.store = DS.Store.create({
  revision: 11,
  adapter: 'Clive.adapter'
});

Clive.deadlineList = Clive.Deadline.find().get('items');

When I run the code I have this error :

Uncaught Error: assertion failed: Your server returned a hash with the key 0 but you have no mapping for it 

Here a jsfidlle with the exemple : http://jsfiddle.net/gilles/6D5BC/

Gilles M
  • 104
  • 1
  • 5
  • Been there.. Maybe [this](http://stackoverflow.com/questions/12182866/ember-data-how-do-mappings-work) will help. Also, read about the JSON format that Ember-Data likes [here](http://emberjs.com/guides/models/the-rest-adapter/). – MilkyWayJoe Jan 04 '13 at 22:48

2 Answers2

5

The "server returned a hash with key 0" thing is because didFindAll() expects a javascript object not a string. So trying again with:

json = {"items":[{"id":93,"title":"title","date":"14-11-2012"}]};
// -> "Your server returned a hash with the key items but you have no mapping for it"

Next step is to transform the object to have naming conventions that ember is expecting. Since your model is named Deadline, use the following:

jsonTransformed = '{"deadlines": [{"id":93,"title":"title 1","date":"14-11-2012"},{"id":94,"title":"title 2","date":"14-11-2012"}]}';

I've added a second record, but you get the idea. Finally you need to change how the Clive.deadlineList variable is being set: Clive.Deadline.find() returns a collection of Clive.Deadline models, so just:

Clive.deadlineList = Clive.Deadline.find()
console.log(Clive.deadlineList.getEach('title'));
// -> title1, title2

Here is an updated jsfiddle with working example: http://jsfiddle.net/mgrassotti/6D5BC/9/

Mike Grassotti
  • 19,040
  • 3
  • 59
  • 57
  • He said he can't modify the JSON from the server. Is there a way to alter the default key value expected by the model? – CraigTeegarden Jan 04 '13 at 19:02
  • 1
    @c4p yes, with custom `adapter` and `serializer`. Not a lot about this on the internet as of now tho. Basically most apps using Ember-Data connecting to existing APIs or your own server in a scenario you can't change the JSON response, you'd have to customize the `adapter` and `serializer` within your `store`. For scenarios which allow you to change the backend (e.g. your own app) you should be find with the built-in `RESTAdapter`. In [this](http://www.youtube.com/watch?v=_6yMxU-_ARs&list=PLLUBPLc28H8c8ihVHkYeRHe8OiB8P5WL0&index=1) video, Tom Dale gives an overview of how it should work – MilkyWayJoe Jan 05 '13 at 00:08
  • Thanks ! Ok , the best solution is to modify the json response – Gilles M Jan 07 '13 at 13:10
1

Another simple solution is to use the following Gem. It will just make your life easier. You wont need to generate or structure the json manually.

gem 'active_model_serializers'

For more help, have a look on this free screencast

Nadeem Yasin
  • 4,493
  • 3
  • 32
  • 41