Note that there are some explanatory texts on larger screens.

plurals
  1. POEmber-Data: How do "mappings" work
    text
    copied!<p>I'm currently trying to put something together with ember + emberdata + router + asp.net web api. Most of it seem to work, however I stuck in an error message I get when ember-data tries to <code>findAll</code> through the adapter for my models.</p> <p>In my backend I have a model like this (C#):</p> <pre><code>public class Genre { [Key] public int Id { get; set; } [Required] [StringLength(50, MinimumLength=3)] public string Name { get; set; } } </code></pre> <p>Which in my app I represent it like this using ember-data:</p> <pre><code>App.Genre = DS.Model.extend({ id: DS.attr("number"), name: DS.attr("string") }).reopenClass({ url: 'api/genre' }); </code></pre> <p>I have also a Store defined in my App using the RESTAdapter like so:</p> <pre><code>App.store = DS.Store.create({ revision: 4, adapter: DS.RESTAdapter.create({ bulkCommit: false }) }); </code></pre> <p>And the store is used in my controller as below:</p> <pre><code>App.GenreController = Ember.ArrayController.extend({ content: App.store.findAll(App.Genre), selectedGenre: null }); </code></pre> <p>The router is defined as</p> <pre><code>App.router = Em.Router.create({ enableLogging: true, location: 'hash', root: Ember.Route.extend({ //... genre: Em.Route.extend({ route: '/genre', index: Ember.Route.extend({ connectOutlets: function (router, context) { router.get('applicationController').connectOutlet('genre'); } }) }), //... }) }) </code></pre> <p>When I run my application, I get the following message for every object that has this same structure:</p> <blockquote> <p>Uncaught Error: assertion failed: Your server returned a hash with the key 0 but you have no mappings</p> </blockquote> <p>For reference, here's the json the service is returning:</p> <pre><code>[ { "id": 1, "name": "Action" }, { "id": 2, "name": "Drama" }, { "id": 3, "name": "Comedy" }, { "id": 4, "name": "Romance" } ] </code></pre> <p>I cannot tell exactly what the problem is and since the assertion is mentioning that I need mapping, I'd like to know:</p> <ol> <li>What this mapping <em>is</em> and how to use it. </li> <li>Since the returned json is an array, should I be using a different type of controller in my app ,or is there anything I should know about when working with this type of json in ember-data? or should I change the JsonFormatter options in the server? </li> </ol> <p>Any help is welcome.</p> <p>I can definitely add more information if you feel this isn't enough to understand the problem.</p> <p><strong>EDIT</strong>: I've changed a few things in my backend and now my <code>findAll()</code> equivalent action in the server serializes the the output as the following json:</p> <pre><code>{ "genres": [ { "id": 1, "name": "Action" }, { "id": 2, "name": "Drama" }, { "id": 3, "name": "Comedy" }, { "id": 4, "name": "Romance" } ] } </code></pre> <p>But I still can't get it to populate my models in the client and my error message has changed to this:</p> <blockquote> <p>Uncaught Error: assertion failed: Your server returned a hash with the key genres but you have no mappings</p> </blockquote> <p>Not sure what else I might be doing wrong.</p> <p>The method that throws this exception is <a href="https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L236-262"><code>sideload</code></a> and checks for the mappings like this:</p> <pre><code>sideload: function (store, type, json, root) { var sideloadedType, mappings, loaded = {}; loaded[root] = true; for (var prop in json) { if (!json.hasOwnProperty(prop)) { continue; } if (prop === root) { continue; } sideloadedType = type.typeForAssociation(prop); if (!sideloadedType) { mappings = get(this, 'mappings'); Ember.assert("Your server returned a hash with the key " + prop + " but you have no mappings", !!mappings); //... </code></pre> <p>This call <code>sideloadedType = type.typeForAssociation(prop);</code> returns <code>undefined</code> and then I get the error message. The method <code>typeForAssociation()</code> checks for the for <code>'associationsByName'</code> key which returns an empty <code>Ember.Map</code>. </p> <p>Still no solution for this at the moment.</p> <p><em><strong>By the way...</em></strong></p> <p>My action is now like this:</p> <pre><code> // GET api/genres public object GetGenres() { return new { genres = context.Genres.AsQueryable() }; } // GET api/genres //[Queryable] //public IQueryable&lt;Genre&gt; GetGenres() //{ // return context.Genres.AsQueryable(); //} </code></pre> <p>I had to remove the original implementation which gets serialized by <a href="http://json.codeplex.com/">json.NET</a> as I could not find config options to produce a json output as Ember-Data expects ( as in <code>{resource_name : [json, json,...]}</code>). Side effect of this is that I've lost built-in OData support, but I'd like to keep it. Does anyone know how could I configure it to produce different json for a collection? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload