Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4838

How to implement polymorphic models in Ember JS?

$
0
0

@amexaCree wrote:

Please does anyone know how to implement polymorphic models with DS.RESTSerializer? So far I have created a model called ‘feed-item’ that has one attribute ‘feedables’ that describes a polymorphic belongs to relationship with one of two different types of models that extend feedable:

model/feed-item.js

import DS from 'ember-data';
export default DS.Model.extend({
  feedable: DS.belongsTo('feedable', { polymorphic: true })
})

The feedable model and models that extend it:

model/feedable.js

import DS from 'ember-data';
export default DS.Model.extend({
  feedItem: DS.belongsTo('feed-item')
})

model/video-item.js

import DS from 'ember-data';
import Feedable from './feedable';
export default Feedable.extend({
  content: DS.attr('string')
})

model/report-item.js

import DS from 'ember-data';
import Feedable from './feedable';
export default Feedable.extend({
  status: DS.attr('number')
})

To get the model for my feed route I do the following in the route:

route/feed.js

model(){
    return this.store.findAll('feed-item')
}

The response that the backend sends looks like so:

 {
    feed_items: [
      {id: 1, feedable: {id: 1, type: 'videoItem'}} ,
      {id: 2, feedable: {id: 2, type: 'reportItem'}} ,
      {id: 3, feedable: {id: 3, type: 'videoItem'}} ,
    ],
    video_items: [
       {id: 1, content: 'some video'},
       {id: 3, content: 'another video'}
    ],
    report_item:  {id: 2, status: 3},
 }

However this is not working for me. Am I missing something? Or is this setup completely wrong. I am using the DS.RESTSerializer in my ember app however I did not do anything special there for normalizing this response. Please any help will be appreciated.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4838

Trending Articles