余烬装置和有许多关系

Ember fixtures and hasMany relations

本文关键字:许多 关系 装置 余烬      更新时间:2023-09-26

由于某些原因,下面的代码产生Error while loading route: TypeError: Cannot read property 'typeKey' of undefined。一切加载正常,直到我在注释装置中添加注释。我假设将注释嵌套在注释fixture中可以工作,因为这是API返回它的方式,但这似乎是断点。

**我应该注意到,我已经尝试将fixture添加到注释模型中,并使用note_id引用回注释。我没有得到一个错误,但我没有得到任何回复显示**

谢谢你的帮助。

模型/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;

模板/notes.hbs

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

模型/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;

看起来我错过了一些东西。

需要async选项的关系设置为true

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

需要在父元素和子元素之间设置关系

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]
    }
  ]
});