Class Ember.Enumerable

Module: ember
Since: vEmber 0.9

This mixin defines the common interface implemented by enumerable objects in Ember. Most of these methods follow the standard Array iteration API defined up to JavaScript 1.8 (excluding language-specific features that cannot be emulated in older versions of JavaScript).

This mixin is applied automatically to the Array class on page load, so you can use any of these methods on simple arrays. If Array already implements one of these methods, the mixin will not override them.

Writing Your Own Enumerable

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

  1. 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 set().

  2. If you must implement nextObject(). See documentation.

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

Using Ember Enumeration with Other Libraries

Many other libraries provide some kind of iterator or enumeration like facility. This is often where the most common API conflicts occur. Ember's API is designed to be as friendly as possible with other libraries by implementing only methods that mostly correspond to the JavaScript 1.8 API.

Show:

Module: ember
returns
this

This property will trigger anytime the enumerable's content changes. You can observe this property to be notified of changes to the enumerables content.

For plain enumerables, this property is read only. Ember.Array overrides this method.

Module: ember
returns
Object
the object or undefined

Helper method returns the first object from a collection. This is usually used by bindings and other parts of the framework to extract a single object if the enumerable contains only one item.

If you override this method, you should implement it so that it will always return the same value each time it is called. If your enumerable contains only one object, this method should always return that object. If your enumerable is empty, this method should return undefined.

1
2
3
4
5
var arr = ["a", "b", "c"];
arr.get('firstObject');  // "a"

var arr = [];
arr.get('firstObject');  // undefined
Module: ember

Becomes true whenever the array currently has observers watching changes on the array.

Module: ember
returns
Object
the last object or undefined

Helper method returns the last object from a collection. If your enumerable contains only one object, this method should always return that object. If your enumerable is empty, this method should return undefined.

1
2
3
4
5
var arr = ["a", "b", "c"];
arr.get('lastObject');  // "c"

var arr = [];
arr.get('lastObject');  // undefined