简体   繁体   中英

How to define an enumeration with ember-model

In my ember-model model I need to set a String attribute from an enumeration. Is it possible with ember-model?

By Example I would like to have a Book model:

App.Book({
    id: Ember.attr(),
    title: Ember.attr( 'String' ),
    author: Ember.attr( 'String' ),
    status: App.BookStatus
});

and App.Book.Status as an enum with 3 possible values "FREE", "BORROW", "LOST" and use it:

var myBook = App.Book.create({
    title:'myBook', 
    author:'fred', 
    status: App.BookStatus.FREE
})

I need the equivalent to the Java Enum feature

public enum BookStatus {
    FREE, BORROW, LOST 
}

class Book {
    BookStatus bookStatus;
}

Book bookInstance = new Book();
bookInstance.bookStatus=BookStatus.LOST;

As stated in the docs:

In Ember.js, an Enumerable is any object that contains a number of child objects, and which allows you to work with those children using the Ember.Enumerable API.

So for example:

var players = ["Alex", "Paul", "Tom"];

players.forEach(function(item, index) {
  console.log('Player %@: %@'.fmt(index+1, item));
});

Would output:

// Player 1: Alex
// Player 2: Paul
// Player 3: Tom

To make your own custom class enumerable, you need two items:

To make your own enumerable class/object in ember you could use the Ember.Enumerable mixin and follow this two requirements:

  • You must have a length property. This property should change whenever the number of items in your enumerable object changes. If you using this with an Ember.Object subclass, you should be sure to change the length property using the built-in set() method.

  • If you must implement nextObject() .

Once you have these two methods implemented, apply the Ember.Enumerable mixin to your class and you will be able to enumerate the contents of your object like any other collection.

Pseudo code:

App.MyEnumObject = Ember.Object.extend(Ember.Enumerable, {
  length: 0,
  nextObject: function() {
    //return the next object of the enumeration
  }
  //more methods and properties
});

See here for more info on enumerable in ember.

Hope it helps.

Edit

Since we are using javascript, what you are trying to do could be done in a much simpler way. For example:

App.bookStatus = {
  FREE: "FREE",
  BORROW: "BORROW",
  LOST: "LOST"
}
  
var myBook = App.Book.create({id: 1, title: 'myBook', author: 'fred', status: App.bookStatus.FREE})

For simple enum's in Ember, just using a simple JavaScript object, as shown at the bottom of @intuitivepixel's answer would suffice, but you can get a bit more functional, Ember-style enums with ember-computed-enum .

It allows getting and setting by more JavaScript-style-friendly 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 particularly useful in templates:

myBook.get('statusIsLost');  // -> true
myBook.get('statusIsFree');  // -> false

Install with ember install ember-computed-enum , then usage is pretty simple

Assuming your model looks something like:

// 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 is returned from the server as one of "FREE" , "BORROW" , or "LOST"

You would add a mixin-style Ember enum like this:

// 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')
});

If you're not able to change the name of the status field to statusCode in the API, and still want to use the name status to access the value within your code, you can remap the model field name using a serializer :

// app/serializers/book.js

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
  attrs: {
    statusCode: 'status'  // map the API's "status" field to the "statusCode" field on the model
  }
});

Note: I've made the examples to reflect current (Feb 2017) ember-cli conventions, rather than the older globals style of the question

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