简体   繁体   中英

Ember fixtures and hasMany relations

For some reason the code below results in Error while loading route: TypeError: Cannot read property 'typeKey' of undefined . Everything loads fine until I add the comments in the Note fixtures. I assume that having the comments nested in the note fixtures would work seeing as that is the way the API would return it, but that seems to be the break point.

** I should note that I have tried to add fixtures to the comments model and with a note_id referencing back to a note. I don't get an error, but I don't get any replies showing up**

Thanks for any help.

models/note.js

import DS from 'ember-data';

var Note = DS.Model.extend({
  content:  DS.attr('string'),
  comments: DS.hasMany('comment'),
});

Note.reopenClass({
  FIXTURES: [
    {
      id: 1,
      content: 'This is the first comment',
      comments: [
        { id: 1, content: 'First comment' },
        { id: 2, content: 'Second comment' },
        { id: 3, content: 'Third comment' }
      ]
    },
    {
      id: 2,
      content: 'This is the second comment',
      comments: [
        { id: 4, content: 'First comment' },
        { id: 5, content: 'Second comment' },
        { id: 6, content: 'Third comment' }
      ]
    }
  ]
});

export default Note;

templates/notes.hbs

{{#each}}
<div>
  <div>{{content}}</div>

  {{#each comments}}
  {{this.content}}
  {{/each}}
</div>
{{/each}}

models/comment.js

import DS from 'ember-data';

var Comment = DS.Model.extend({
  content:   DS.attr('string'),
  timestamp: DS.attr('date'),

  note: DS.belongsTo('note')
});

Comment.reopenClass({
  FIXTURES: [
    { id: 1, content: 'First comment',  note_id: 1 },
    { id: 2, content: 'Second comment', note_id: 1 },
    { id: 3, content: 'Third comment',  note_id: 1 }
  ]
});

export default Comment;

Looks like I was missing a couple things.

need the async options in the relationships set to true

comments: DS.hasMany('comment', { async: true })

needed to set a relationship in the parent to the children

Note.reopenClass({
  FIXTURES: [
    {
      id: 1,
      content: 'This is the first comment',
      comments: [1, 2, 3]
    },
    {
      id: 2,
      content: 'This is the second comment',
      comments: [4, 5, 6]
    }
  ]
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM