修订版12中的自定义映射

时间:2013-04-12 08:44:14

标签: ember.js ember-data

我已升级到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!'
  ]
]

1 个答案:

答案 0 :(得分: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')
});

那应该为你做。