简体   繁体   中英

Defining an enum on ember DS.Model

I'm wondering what the best solution could be for using ActiveRecord 's enum alongside DS.model in Ember Data.

For example, if I have a enum in my Rails model:

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

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

My first thought would be to define an integer type on DS.model:

status: DS.attr('number')

However, using Ember Data and ActiveModel Serializer, the serializer serializes these enums as strings, so json ends up with:

{status: 'draft'}

So should this be a DS.attr('string') or is there a way to specify an enumeration in ember data?

Yes, you can use it as a string or you can convert it to a different type in the Serializer , however, it's probably easiest to leave it a string. You can always add some computed properties to your model:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM