如何在夹具中设置动态模型属性



正如标题所述,我在夹具图层上创建动态属性时遇到了麻烦。

下面是一个示例模型:

App.Pokeball = DS.Model.extend({
    name: DS.attr('string'),
    ballRate: DS.attr('number'),
    battleAttributes: DS.belongsTo('battleAttributes')
});

还有我的灯具:

App.Pokeball.reopenClass({
    FIXTURES : [
        {
            id: 1,
            name: 'PokeBall',
            ballRate: 1
        },
        {
            id: 23,
            name: 'Dusk Ball',
            ballRate: function() {
                // Some logic that applies only model #23
                return 2;
            }.property('battleAttributes')
        }
    ]
});

我在网上搜索,试图找到正确的方法,但反而陷入了死胡同。 :(

这是对灯具的无效使用。它们旨在表示服务器上传递给应用程序并转换为 Ember 数据模型的 JSON(或其他任何内容)。JSON 不能表示计算属性的概念,它用于纯数据。

我不明白你的用例,所以我可能会很远,似乎你应该在模型上使用计算属性:

App.Pokeball = DS.Model.extend({
  name: DS.attr('string'),
  ballRate: DS.attr('number'),
  battleAttributes: DS.belongsTo('battleAttributes'),
  adjustedBallRate: function() {
    if (this.get('battleAttributes.whateverPropertyCausesThisToChange') == 'special value') {
      return 2;
    }
    else {
      return this.get('ballRate');
    }
  }.property('battleAttributes.whateverPropertyCausesThisToChange')
});

最新更新