Ember.js Rest Adapter: display JSON without root (.NET Web API)

I have an existing service written using the .NET Web API.

As an example, this service returns JSON in the following format:

[ { "id": 1, "name": "John" }, { "id": 2, "name": "Jane" } ] 

However, according to the documentation

Because of this, Ember returns the following error: The server returned a hash with key 0, but there is no mapping for it

In no case do I plan to change my API and how it returns data.

Can I get Ember.js (latest version) to work with existing data returned by my service? And if so, how can I implement this?

+8
json javascript asp.net-web-api ember-data
source share
3 answers

Ember is very flexible in this sense, providing the ability to extend the adapter and serializer to integrate your application with any API.

You should get the WebAPIAdapter, which is part of the Ember.js web API template.

In addition, you can take a look at this project . I wrote as an example, based on the same template (with some changes that I made on my own). It is still under development and does not contain all the best practices (for now), but I would say that this is a valid example.

You should also study this repo / library (you can install it through NuGet ), which allows you to precompile Handlebars templates directly into the Ember.TEMPLATES collection.

+5
source share

in web api just returns the new {object}

 var persons = _personbService.GetPeople; return new { persons }; 

this will wrap your object in an object with the same name. If you need to call an object for something else, just do

 new { SomeOtherName = persons; } 
0
source share

I had this problem in ember and I found the best solution for me - create a new serializer and override the normalizePayload method. code below:

 export default DS.RESTSerializer.extend({ normalizePayload: function(payload) { var root = "posts"; var output = {}; output[root] = payload; return output; 

}});

This wraps up the initial answer and adds root to it, hope this helps!

0
source share

All Articles