Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

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

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

However, as per the Ember.js Rest Adapter documentation, Ember would expect JSON in the following format:

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

Because of this, Ember is returning the following error: Your server returned a hash with the key 0 but you have no mapping for it

By no means do I plan on changing my service API and how it returns data.

Would it be possible to get Ember.js (latest version) to work with the existing data my service is returning? And, if so, how can I implement that?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Ember is very flexible in that sense, giving the ability to extend the adapter and serializer in order to integrate your app with any backend API.

You should get the WebAPIAdapter, which is part of the Ember.js Template for Web API.

Additionally, you might wanna take a look into this project I wrote as an example, based on that same template (with some modifications I did on my own). It's still under development and it doesn't feature all the best practices (yet), but I'd say it's a valid example.

You should also take a look into this repo / library (You can install it via NuGet too), which allows you to pre-compile your Handlebars templates directly into Ember.TEMPLATES collection.

share|improve this answer
    
Thanks! In your Ember-Contact-Management-WebAPI, are you doing any special adapting or serialization of data to work with Ember? I noticed your ContactController returns IEnumerable<ContactDto> or ContactDto for gets. This is pretty much what my Web API controller exposes, however, the format is not correct on my end. –  Grand Master T Apr 5 '13 at 17:36
1  
Check this directory, you'll see the definition of the Web API Adapter & WebAPISerializer, which handles the JSON payload integration with Ember-Data. These classes are extending the built-in RESTAdapter and RESTSerializer. –  MilkyWayJoe Apr 5 '13 at 17:43
    
That's exactly what I needed. For some reason however, using DS.WebAPIAdapter, Ember now hits my REST service url as a singular noun. For instance, before using the RestAdapter, it would hit localhost/api/things. Now it hits localhost/api/thing. Any ideas? –  Grand Master T Apr 5 '13 at 18:04
    
You should set plurals in your adapter. This syntax was valid for revision 4, but we're currently in revision 12. I honestly don't know if this part of the API has changed –  MilkyWayJoe Apr 5 '13 at 18:12
    
I know it's weird, but most APIs in .NET don't pluralize controllers, that's why the WebAPIAdapter#pluralize simply returns the name of the model the way it is defined, just lower cased. You can either remove this method from the WebAPIAdapter (fallback to RESTAdapter#pluralize) or edit it to return + 's'... –  MilkyWayJoe Apr 5 '13 at 18:21

in the web api just return new { object }

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

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

new { SomeOtherName = persons; }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.