Drew Larson
Drew Larson

Reputation: 720

Ember Data and mapping JSON objects

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

I'm not very good with javascript yet so I had a hard time understanding the hooks in the serialize_json.js or maybe I should be using mapping (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 totally makes when you look at my code for the data store:

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

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

Upvotes: 6

Views: 6683

Answers (3)

Gevious
Gevious

Reputation: 3252

Ember is fairly opinionated about how things are done. Ember data is no exception. The Ember team works towards certain standards that it thinks is best, which is, in my opinion, a good thing.

Check out this post on where ember is going. TL;DR because there are so many varying implementations of api calls, they're setting their efforts towards supporting the JSON API.

From my understanding, there is no easy way to do what you're asking. Your best bet is to write your own custom adapter and serialized. This shouldn't be too hard to do, and has been done before. I recommend you having a look at the Tastypie adapter used for Python's Django Tastypie

Upvotes: 1

Baruch
Baruch

Reputation: 1133

The way to accomplish this is with a custom serializer. If all your data is returned from the server in this format you could simply create ApplicationSerializer like this:

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

That should allow Ember Data to consume your API seamlessly.

Upvotes: 1

Willem de Wit
Willem de Wit

Reputation: 8742

All root properties you return in your JSON result are mapped to a DS.Model in Ember Data. You should not return properties that are not modelled or you should model them.

If you want to get rid of the error you should make an empty model for the properties you don't use.

Read more here

Why are you returning properties you don't want to use? Or is it out of your control?

Upvotes: 3

Related Questions