2014-08-31 40 views
1

我想知道什麼最好的解決方案可以使用ActiveRecordenum與Ember數據中的DS.model一起。定義在餘燼上的枚舉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') 

但是,使用灰燼Data和ActiveModel Serializer,串行器將這些枚舉序列化爲字符串,所以json最終以:

{status: 'draft'} 

所以這應該是DS.attr('string')還是有一種方法來指定枚舉數據中的枚舉?

回答

2

是的,您可以將它用作字符串,或者可以將其轉換爲Serializer中的不同類型,但是,最簡單的方法是將其保留爲字符串。您可以隨時將一些計算出的屬性添加到您的模型中:

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