For a simple listing in Ember, itβs enough to just use a simple JavaScript object, as shown below @intuitivepixel answer , but you can get a little more functional, enum-style enums with ember-computed-enum .
It allows you to get and configure more convenient JavaScript-style names:
myBook.get('status'); // -> 'free' myBook.set('status', 'lost'); myBook.get('status'); // -> 'lost' myBook.save(); // -> saves `status: "LOST"` to the server
but also adds enumNameIsValue properties that are especially useful in templates:
myBook.get('statusIsLost'); // -> true myBook.get('statusIsFree'); // -> false
Install with ember install ember-computed-enum , then using is pretty simple
Assuming your model looks something like this:
// app/models/book.js import DS from 'ember-data'; export default DS.Model.extend({ title: DS.attr('string'), author: DS.attr('string'), statusCode: DS.attr('string') });
Where statusCode returned from the server as one of "FREE" , "BORROW" or "LOST"
You added mixin-style Ember enum as follows:
// app/models/book.js import DS from 'ember-data'; import { computedEnumMixin } from 'ember-computed-enum'; BOOK_STATUS_ENUM = { free: "FREE", borrow: "BORROW", lost: "LOST" }; export default DS.Model.extend( computedEnumMixin('status', 'statusCode', BOOK_STATUS_ENUM), { title: DS.attr('string'), author: DS.attr('string'), statusCode: DS.attr('string') });
Note. I gave examples to reflect the current (February 2017) ember-cli conventions, rather than the style of older question globals.