在ember DS.Model上定义枚举

时间:2014-08-31 03:43:47

标签: ruby-on-rails ruby ember.js enums ember-data

我想知道在Ember Data中使用ActiveRecord的{​​{3}}以及DS.model的最佳解决方案是什么。

例如,如果我的Rails模型中有枚举:

# in the migration
t.integer :status, default: 0

# in the model
enum status: [:draft, :in_wizard, :published, :archived]

我的第一个想法是在DS.model上定义一个整数类型:

status: DS.attr('number')

但是,使用Ember Data和ActiveModel Serializer,序列化程序将这些枚举序列化为字符串,因此json最终得到:

{status: 'draft'}

那么这应该是DS.attr('string')还是有办法在ember数据中指定枚举?

1 个答案:

答案 0 :(得分:2)

是的,您可以将其用作字符串,也可以将其转换为Serializer中的其他类型,但是,最简单的方法是将其保留为字符串。您始终可以向模型添加一些计算属性:

isDraft: Ember.computed.equal('status', 'draft'),
isInWizard: Ember.computed.equal('status', 'in_wizard'),
// etc...