How to set display on Ember-Data

I'm starting to work with ember data, and I have a problem displaying data

Here is an example of my code (I put jsonTest as an example of data received from the backend, I do not work on the backend, and I can not change 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's jsfidlle with an example: http://jsfiddle.net/gilles/6D5BC/

+4
source share
2 answers

"The server returned the hash with key 0" because didFindAll () expects the javascript object to be not a string. Try again:

 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" 

The next step is to transform the object to have the naming conventions that ember expects. Since your model is called 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 added a second entry, but you get the idea. Finally, you need to change the way you set the Clive.deadlineList variable: Clive.Deadline.find() returns a collection of Clive.Deadline models, so it's simple:

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

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

+5
source

Another simple solution is to use the following Gem. It will simply simplify your life. You do not need to create or structure json manually.

 gem 'active_model_serializers' 

For more information, see the free screencast.

+1
source

All Articles