Ember.js 夹具中的嵌入记录错误:“;无法调用方法';拥有自己的财产';未定义的

Ember.js 夹具中的嵌入记录错误:“;无法调用方法';拥有自己的财产';未定义的,ember.js,ember-data,Ember.js,Ember Data,我正在尝试将装置加载到具有嵌入记录的应用程序中 从服务器加载数据(使用DS.RESTAdapter)是可行的,但当我尝试通过DS.FixtureAdapter加载数据时,情况并非如此 型号: App.Post = DS.Model.extend title: DS.attr('string') comments: DS.hasMany('App.Comment') App.Comment = DS.Model.extend text: DS.attr('string') # I

我正在尝试将装置加载到具有嵌入记录的应用程序中

从服务器加载数据(使用
DS.RESTAdapter
)是可行的,但当我尝试通过
DS.FixtureAdapter
加载数据时,情况并非如此

型号:

App.Post = DS.Model.extend
  title: DS.attr('string')
  comments: DS.hasMany('App.Comment')

App.Comment = DS.Model.extend
  text: DS.attr('string')
  # I'm not specifying DS.belongsTo('post') because comments could also exist 
  # with other objects.
  # Anyway, it does not work even with it. 
App.Adapter = DS.FixtureAdapter.extend()
App.Adapter.map App.Post,
  comments:
    embedded: 'always'
App.store = DS.Store.create
  revision: 11
  adapter: App.Adapter.create()
App.Comment.FIXTURES = []

App.Post.FIXTURES = [
  {
    id: "1"
    title: "My post"
    comments: [{
      id: "1"
      text: "My first comment" 
    }, {
      id: "2"
      text: "My second comment"
    }]
  }
]
适配器:

App.Post = DS.Model.extend
  title: DS.attr('string')
  comments: DS.hasMany('App.Comment')

App.Comment = DS.Model.extend
  text: DS.attr('string')
  # I'm not specifying DS.belongsTo('post') because comments could also exist 
  # with other objects.
  # Anyway, it does not work even with it. 
App.Adapter = DS.FixtureAdapter.extend()
App.Adapter.map App.Post,
  comments:
    embedded: 'always'
App.store = DS.Store.create
  revision: 11
  adapter: App.Adapter.create()
App.Comment.FIXTURES = []

App.Post.FIXTURES = [
  {
    id: "1"
    title: "My post"
    comments: [{
      id: "1"
      text: "My first comment" 
    }, {
      id: "2"
      text: "My second comment"
    }]
  }
]
商店:

App.Post = DS.Model.extend
  title: DS.attr('string')
  comments: DS.hasMany('App.Comment')

App.Comment = DS.Model.extend
  text: DS.attr('string')
  # I'm not specifying DS.belongsTo('post') because comments could also exist 
  # with other objects.
  # Anyway, it does not work even with it. 
App.Adapter = DS.FixtureAdapter.extend()
App.Adapter.map App.Post,
  comments:
    embedded: 'always'
App.store = DS.Store.create
  revision: 11
  adapter: App.Adapter.create()
App.Comment.FIXTURES = []

App.Post.FIXTURES = [
  {
    id: "1"
    title: "My post"
    comments: [{
      id: "1"
      text: "My first comment" 
    }, {
      id: "2"
      text: "My second comment"
    }]
  }
]
固定装置:

App.Post = DS.Model.extend
  title: DS.attr('string')
  comments: DS.hasMany('App.Comment')

App.Comment = DS.Model.extend
  text: DS.attr('string')
  # I'm not specifying DS.belongsTo('post') because comments could also exist 
  # with other objects.
  # Anyway, it does not work even with it. 
App.Adapter = DS.FixtureAdapter.extend()
App.Adapter.map App.Post,
  comments:
    embedded: 'always'
App.store = DS.Store.create
  revision: 11
  adapter: App.Adapter.create()
App.Comment.FIXTURES = []

App.Post.FIXTURES = [
  {
    id: "1"
    title: "My post"
    comments: [{
      id: "1"
      text: "My first comment" 
    }, {
      id: "2"
      text: "My second comment"
    }]
  }
]
控制台中

post = App.store.find(App.Post, 1);

comments = post.get("comments");
console.log(comments.get('length')); // => 1

firstComment = comments.get('firstObject');
console.log(firstComment.get('id')); // => undefined
console.log(firstComment.get('name')); // => TypeError: Cannot call method `hasOwnProperty` of undefined
我认为这个问题与,也许与

使用的余烬版本:

  • 余烬数据
  • 余烬
编辑


我还写了一篇文章。

嗯,起初我是在猜测没有id的固定装置,但可能没有

App.Adapter = DS.FixtureAdapter.extend()
App.Adapter.map App.Product,
  groups:
    embedded: 'always' 
还应包含应用程序。Post是否始终嵌入注释

如果这不能解决这个问题,我怀疑fixture适配器不能很好地与embedded一起工作,您也必须定义注释

更新:与@tomdale讨论后,不会对fixture适配器进行任何更改,以使其能够加载嵌入式记录。因此,我只能提出以下建议:

顺便说一句,汤姆说的是:

我不相信fixture适配器支持嵌入式记录 是否有充分的理由说明它需要这样做? 它也不支持带下划线的属性名称 fixture适配器的思想不是模仿来自服务器的JSON负载 它以余烬数据所期望的格式提供存根数据 因此,关系不会嵌入,属性名称会用大小写,等等`


对不起,我把我真正的用法翻译成更“经典”的用法时出错了。
Adapter.map
已包含
注释
,始终嵌入在
App.Post
中。我已经更新了我的问题。我真的想保留与服务器发送的数据相同的装置,以便这些装置可以用来描述API规范请您提交一个JSFIDLE?我已经更新了我的问题并添加了JSFIDLE和一个失败的测试。如果你认为这是一个bug,我可以为这个失败的测试做一个PR,但我不知道如何修复它。无论如何,感谢您的时间:-)事实上,夹具适配器中应该有一个bug。我没有时间尝试太多,但我认为在fixtureAdapter.find()方法中,应该调用adapter.didFindRecord()或adapter.load(),因为store.load()不经过关联。有可能知道为什么fixture适配器不会处理嵌入的记录吗?我相信让fixture与服务器响应匹配会很有趣。不管怎么说,我听从了你的建议,找到了一个解决办法:,用它做公关会是个好主意吗?