How to define an enumeration using ember-model

In my ember-model, I need to set the String attribute from an enumeration. Is this possible with the ember model?

As an 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 enumeration with three possible values "FREE", "BORROW", "LOST" and use it:

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

I need Java Enum function equivalent

 public enum BookStatus { FREE, BORROW, LOST } class Book { BookStatus bookStatus; } Book bookInstance = new Book(); bookInstance.bookStatus=BookStatus.LOST; 
+4
source share
2 answers

As stated in the docs:

In Ember.js, Enumerable is any object that contains multiple children and that allows you to work with these 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)); }); 

Output:

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

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

To create your own enumerated class / object in ember, you can use mixa Ember.Enumerable and fulfill the following two requirements:

  • You must have a length property. This property should change whenever the number of elements in your enumerated object changes. If you use this with a subclass of Ember.Object , you must definitely change the length property using the built-in set() method.

  • If you must implement nextObject() .

Once you implement these two methods, apply mixa Ember.Enumerable to your class, and you can list the contents of your object, like any other collection.

Pseudocode:

 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 information on enber enumerations.

Hope this helps.

Edit

Since we use javascript, what you are trying to do can be made much simpler. For instance:

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

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.

0
source

Source: https://habr.com/ru/post/1495361/


All Articles