mausinc
mausinc

Reputation: 537

WEB API JSON Result with object name

I am working with Web Api and Ember JS, all worked well until i started using Ember Data, which seems to need the object name included in the json.

How can i add this?

Currently getting :

[
  {
    "id": 1,
    "title": "maxima",
    "subTitle": null,
    "description": "Maxima de boot",
    "image1": null,
    "image2": null,
    "active": false,
    "skipper": null
  },
  {
    "id": 2,
    "title": "beatrix",
    "subTitle": null,
    "description": "Beatrix de boot",
    "image1": null,
    "image2": null,
    "active": false,
    "skipper": null
  }
]

Thanks so much!

Upvotes: 1

Views: 487

Answers (3)

djikay
djikay

Reputation: 10628

I'm pretty new at this and haven't worked with Ember before, but... Instead of returning an array of "stuff" objects, can you not modify the code to return an object that internally just contains an array of "stuff" objects? Then, I expect the default json serializer should take care of this for you.

Upvotes: 0

Kingpin2k
Kingpin2k

Reputation: 47367

Having worked with Web API and Ember Data plenty you will find it easier to fix the json client side using a serializer. Assuming you're finding posts, `this.store.find('post') you would create a custom serializer for it:

App.PostSerializer = DS.RESTSerializer.extend({
   extractArray: function(store, type, payload) {
    payload = {posts: payload};
    return this._super(store, type, payload);
  }
});

Example: http://emberjs.jsbin.com/OxIDiVU/623/edit

Read more about it (and other methods) here http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_extractArray and here https://github.com/emberjs/data/blob/master/TRANSITION.md

Upvotes: 2

MartinElvar
MartinElvar

Reputation: 5804

Ember data doesn't know how to map that data, your json should look like this instead.

{ blogs: [ 
  { "id": 1, "title": "maxima", "subTitle": null, "description": "Maxima de boot", "image1": null, "image2": null, "active": false, "skipper": null },
  { "id": 2, "title": "beatrix", "subTitle": null, "description": "Beatrix de boot", "image1": null, "image2": null, "active": false, "skipper": null } ] 
}

Now ember knows that the objects from the server, should be mapped to the blogs model.

Upvotes: 1

Related Questions