1

The json I get from the API looks like this:

[
  {
    "id": "1",
    "title": "title1"
  },
  {
    "id": "2",
    "title": "title2"
  },
]

Unfortunately I can't change the API, so how do I get it to work with the RESTAdapter?

I tried with this code from this post :

App.ApplicationSerializer = DS.RESTSerializer.extend({
    normalizePayload: function(type, payload) {
        return { posts: payload };
    }
});

But I get the error " Error while loading route: Error: No model was found for 'post' ".

Which I don't understand.

This is my posts Route.

App.PostsRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('posts');
  }
});
Community
  • 1
  • 1

2 Answers2

1

You have a number of typeos here.

First since you are dealing with posts, it is probably best to use the PostSerializer.

App.PostSerializer = DS.RESTSerializer.extend({
  normalizePayload: function(type, payload) {
    return { posts: payload };
  }
});

And when you are requesting models from the server, you want to use the model name, so you would use post (not posts).

App.PostsRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('post');
  }
});
Ryan
  • 3,594
  • 1
  • 24
  • 23
  • You are right, it works now. I was a little confused with the posts/post thing. I got it now. Still trying to wrap my head around it. Thanks. –  May 20 '14 at 12:36
  • @Ryan , Hallelujah! :-) Works great for me when requesting records. How about for adding records? It's not working for me and I'm wondering if a corresponding change needs to be made for PUT requests. – hourback Sep 25 '14 at 22:13
0

Neither normalizePayload nor normalize is working for me. What I am doing is:

// app/serializers/application.js
import DS from 'ember-data';

export default DS.RESTSerializer.extend({
    extractArray: function(store, type, payload) {
        var payloadTemp = {}
        payloadTemp[type.typeKey] = payload;
        return this._super(store, type, payloadTemp);
    },
    extractSingle: function(store, type, payload, id) {
        var payloadTemp = {}
        payloadTemp[type.typeKey] = [payload];
        return this._super(store, type, payloadTemp, id);
    }
});
Lei Cao
  • 457
  • 6
  • 13