mercredi 19 juillet 2017

Accessing Ember data has-many relationship on template

I have a rails back-end, serving data as per the JSON API standards. I have two models: song and review.

class Song < ApplicationRecord
  has_many :reviews
end

class Review < ApplicationRecord
  belongs_to :song
end

This is the JSON response for the API endpoint.

{
  "data": {
      "type": "songs",
      "id": "2",
      "attributes": {
          "id": 2,
          "artist": "Parliament Funkadelic",
          "title": "Chuck Norris doesn't need a debugger, he just stares down the bug until the code confesses."
      },
      "links": {
          "self": "/songs/2"
      },
      "relationships": {
          "reviews": {
              "links": {
                  "self": "/songs/2/relationships/reviews",
                  "related": "/songs/2/reviews"
              },
              "data": [
                  {
                      "type": "reviews",
                      "id": "2"
                  }
              ]
          }
      }
  },
  "included": [
      {
          "type": "reviews",
          "id": "2",
          "attributes": {
              "id": 2,
              "author": "Underworld",
              "content": "Helvetica leggings mixtape keffiyeh lo-fi."
          },
          "links": {
              "self": "/reviews/2"
          },
          "relationships": {
              "song": {
                  "links": {
                      "self": "/reviews/2/relationships/song",
                      "related": "/reviews/2/song"
                  }
              }
          }
      }
  ]
}

These are my Ember models:

for song:

import DS from 'ember-data';

export default DS.Model.extend({
  artist: DS.attr('string'),
  title: DS.attr('string'),
  reviews: DS.hasMany('review', {async: true})
});

for review

import DS from 'ember-data';

export default DS.Model.extend({
  author: DS.attr('string'),
  content: DS.attr('string'),
  song: DS.belongsTo('song', {async: true})
});

This is my route handler:

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(params) {
    return this.store.findRecord('song', params.song_id, {include: 'reviews'});
},
(...)

I am unable to access the reviews for a song on the template by doing:

<ul>
  
    <li></li>
  
</ul>

Also, the data for the reviews exists in the ember console. What am I missing?




Aucun commentaire:

Enregistrer un commentaire