1
votes

In order to use polymorphic relationships in Ember.js, you need to configure your adapter to recognize an alias for the polymorphic model, as documented here:

DS.RESTAdapter.configure('App.Post', {
  alias: 'post'
});

Unfortunately, this approach no longer works with Ember Data 1.0Beta, as you can no longer configure adapters. Instead, you have to extend them. Simply doing this doesn't work, however:

DS.ActiveModelAdapter.extend('App.Post', {
  alias: 'post'
});

It throws the error:

Expected hash or Mixin instance, got [object String]

This section of Ember-Data's transition guide goes into detail about the new approach to adapters and serializers. I'm not sure how to translate that advice for something like alias: 'post', however. The guide goes into a great deal of detail about how payloads are processed, but I don't know where the alias was suppose to fit into that processing.

1

1 Answers

1
votes

That's out of date at the top, see the polymorphism portion in the transition documentation https://github.com/emberjs/data/blob/master/TRANSITION.md#polymorphic-relationships

Polymorphic relationships

Polymorphic types are now serialized with a json key of the model name + "Type"

For example given the polymorphic relationship:

 App.Comment = DS.Model.extend({
   message: DS.belongsTo('message', {
     polymorphic: true
   })
 });

Ember Data 0.13

 {
   "message": 12,
   "message_type": "post"
 }

Ember Data 1.0.beta.3:

 {
   "message": 12,
   "messageType": "post"
 }

On another note if you ever see that error again, it's complaining about this

 DS.ActiveModelAdapter.extend('App.Post', {
   alias: 'post'
 });

the first parameter of any extension of an ember object expects a hash or mixin, and you are sending it a string. Aka it wants an actual Class (it needs to have been defined before you get to this portion of your code).

 DS.ActiveModelAdapter.extend(App.Post, {
   alias: 'post'
 });