Class Ember.Array

Module: ember
Since: vEmber 0.9.0

This module implements Observer-friendly Array-like behavior. This mixin is picked up by the Array class as well as other controllers, etc. that want to appear to be arrays.

Unlike Ember.Enumerable, this mixin defines methods specifically for collections that provide index-ordered access to their contents. When you are designing code that needs to accept any kind of Array-like object, you should use these methods instead of Array primitives because these will properly notify observers of changes to the array.

Although these methods are efficient, they do add a layer of indirection to your application so it is a good idea to use them only when you need the flexibility of using both true JavaScript arrays and "virtual" arrays such as controllers and collections.

You can use the methods defined in this module to access and modify array contents in a KVO-friendly way. You can also be notified whenever the membership of an array changes by changing the syntax of the property to .observes('*myProperty.[]').

To support Ember.Array in your own class, you must override two primitives to use it: replace() and objectAt().

Note that the Ember.Array mixin also incorporates the Ember.Enumerable mixin. All Ember.Array-like objects are also enumerable.

Show:

Module: ember

Returns a special object that can be used to observe individual properties on the array. Just get an equivalent property on this object and it will return an enumerable that maps automatically to the named key on the member objects.

If you merely want to watch for any items being added or removed to the array, use the [] property instead of @each.

Module: ember

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

Module: ember
returns
this

This is the handler for the special array content property. If you get this property, it will return this. If you set this property it a new array, it will replace the current content.

This property overrides the default property defined in Ember.Enumerable.

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
Module: ember

Your array must support the length property. Your replace methods should set this property whenever it changes.