0

我已经升级到 Ember-Data rev 12,但不知道如何加载我的 Fixture。我尝试key' and在模型上使用 sideLoadAs`,但没有效果。

模型:

Sks.User = DS.Model.extend
  kudosReceived: DS.hasMany('Sks.KudoReceived', key: 'kudo_received_ids', sideLoadAs: 'kudos_received')
  name: DS.attr("string")
  email: DS.attr("string")

夹具:

Sks.User.FIXTURES = [
  id: 1
  name: 'Joe Doe'
  email: 'joe.doe@gmail.com'
  kudo_received_ids: [1, 2]
  kudos_received: [
    id: 1
    value: 2
    comment: 'Because you are nice to me!'
  ,
    id: 2
    value: 3
    comment: 'It was awesome!'
  ]
]
4

1 回答 1

0

您的固定装置需要是一个对象数组,而不仅仅是一个数组。还可以尝试将它们拆分为每种类型的单独固定装置:

Sks.User.FIXTURES = [{
    id: 1,
    name: 'Joe Doe',
    email: 'joe.doe@gmail.com',
    kudos: [1,2]
}];

Sks.Kudos.FIXTURES = [{
    id: 1,
    value: 2,
    comment: 'Because you are nice to me!'
}, {
    id: 2,
    value: 3,
    comment: 'It was awesome!'
}];

然后你应该需要一个简单的关系来将两者联系在一起:

Sks.User = Ds.Model.extend({
    name: DS.attr('string'),
    email: DS.attr('string'),
    kudos: DS.hasMany('Sks.Kudos')
});

Sks.Kudos = Ds.Model.extend({
    value: DS.attr('number'),
    comment: DS.attr('string'),
    user: DS.belongsTo('Sks.User')
});

那应该为你做。

于 2013-04-12T11:41:52.963 回答