Ember Data and display of JSON objects

I really searched, and I did not find a decent example of using a serializer to get objects from a formatted JSON response. My reason for not changing the format of the JSON response is here http://flask.pocoo.org/docs/security/#json-security .

I am not very good at javascript, so it was hard for me to understand the hooks in serialize_json.js or maybe I should use matching (I just don't know). So, here is an example of my JSON response for many objects:

{ "total_pages": 1, "objects": [ { "is_completed": true, "id": 1, "title": "I need to eat" }, { "is_completed": false, "id": 2, "title": "Hey does this work" }, { "is_completed": false, "id": 3, "title": "Go to sleep" }, ], "num_results": 3, "page": 1 } 

When ember-data tries to use this, I get the following error:

 DEBUG: ------------------------------- DEBUG: Ember.VERSION : 1.0.0-rc.1 DEBUG: Handlebars.VERSION : 1.0.0-rc.3 DEBUG: jQuery.VERSION : 1.9.1 DEBUG: ------------------------------- Uncaught Error: assertion failed: Your server returned a hash with the key total_pages but you have no mapping for it 

Which completely does when you look at my data warehouse code:

 Todos.Store = DS.Store.extend({ revision: 12, adapter: DS.RESTAdapter.create({ mappings: {objects: "Todos.Todo"}, namespace: 'api' }) }); 

My question is: how do I work with total_pages , num_results and page ? And for the deal, I mean ignore it, so I can just map the array of objects .

+6
source share
3 answers

All root properties that you return as a result of JSON are displayed on DS.Model in Ember Data. You must not return properties that are not modeled, or you must model them.

If you want to get rid of the error, you must create an empty model for properties that you are not using.

More here

Why are you returning properties that you do not want to use? Or is it not under your control?

+3
source

Amber is pretty stubborn about how things are. Ember data is no exception. The Ember team is working on certain standards, which, in his opinion, are the best, which, in my opinion, is good.

Check out this post where ember goes. TL DR, because there are so many different implementations of api calls, they are making efforts to support the JSON API .

In my opinion, there is no easy way to do what you ask. It’s best to write your own adapter and serialize. It should not be too difficult to do, and it has been done before. I recommend you take a look at the Tastypie adapter used for Python Django Tastypie

+1
source

A way to accomplish this is with a special serializer. If all data is returned from the server in this format, you can simply create an ApplicationSerializer as follows:

 DS.RESTSerilizer.extend({ normalizePayload: function(type, payload) { delete payload.total_pages; delete payload.num_results; delete payload.page; return payload; } }); 

This should allow Ember Data to use your API seamlessly.

+1
source

All Articles