Class EmberArray
publicimport EmberArray from '@ember/array'; |
This mixin implements Observer-friendly Array-like behavior. It is not a concrete implementation, but it can be used up by other classes that want to appear like arrays.
For example, ArrayProxy is a concrete classes that can be instantiated to implement array-like behavior. Both of these classes use the Array Mixin by way of the MutableArray mixin, which allows observable changes to be made to the underlying array.
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 using .observes('myArray.[]')
.
To support EmberArray
in your own class, you must override two
primitives to use it: length()
and objectAt()
.
@each public
Defined in packages/ember-runtime/lib/mixins/array.js:1205
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 array that maps automatically to the named key on the member objects.
@each
should only be used in a non-terminal context. Example:
1 2 3 |
myMethod: computed('posts.@each.author', function(){ ... }); |
If you merely want to watch for the array being changed, like an object being
replaced, added or removed, use []
instead of @each
.
1 2 3 |
myMethod: computed('posts.[]', function(){ ... }); |
[] public
Defined in packages/ember-runtime/lib/mixins/array.js:250
- 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 to a new array, it will replace the current content.
firstObject Object | undefined public
Defined in packages/ember-runtime/lib/mixins/array.js:269
- returns
- Object | undefined
- The first object in the array
The first object in the array, or undefined
if the array is empty.
hasArrayObservers public
Defined in packages/ember-runtime/lib/mixins/array.js:470
Becomes true whenever the array currently has observers watching changes on the array.
lastObject Object | undefined public
Defined in packages/ember-runtime/lib/mixins/array.js:280
- returns
- Object | undefined
- The last object in the array
The last object in the array, or undefined
if the array is empty.
length public
Defined in packages/ember-runtime/lib/mixins/array.js:188
Required. You must implement this method to apply this mixin.
Your array must support the length
property. Your replace methods should
set this property whenever it changes.