Class: SC.ArrayController


Extends SC.Array, SC.Controller, SC.SelectionSupport.

An ArrayController provides a way for you to publish an array of objects for CollectionView or other controllers to work with. To work with an ArrayController, set the content property to the array you want the controller to manage. Then work directly with the controller object as if it were the array itself.

When you want to display an array of objects in a CollectionView, bind the "arrangedObjects" of the array controller to the CollectionView's "content" property. This will automatically display the array in the collection view.

Defined in: array.js

Since:
SproutCore 1.0

Field Summary

Instance Methods

Field Detail

allowsSingleContent Boolean

Set to YES if you want the controller to wrap non-enumerable content in an array and publish it. Otherwise, it will treat single content like null content.

content SC.Array

The content array managed by this controller.

You can set the content of the ArrayController to any object that implements SC.Array or SC.Enumerable. If you set the content to an object that implements SC.Enumerable only, you must also set the orderBy property so that the ArrayController can order the enumerable for you.

If you set the content to a non-enumerable and non-array object, then the ArrayController will wrap the item in an array in an attempt to normalize the result.

destroyOnRemoval Boolean

Set to YES if you want objects removed from the array to also be deleted. This is a convenient way to manage lists of items owned by a parent record object.

Note that even if this is set to NO, calling destroyObject() instead of removeObject() will still destroy the object in question as well as removing it from the parent array.

isEditable Boolean

Makes the array editable or not. If this is set to NO, then any attempts at changing the array content itself will throw an exception.

orderBy String|Array|Function

Used to sort the array.

If you set this property to a key name, array of key names, or a function, then then ArrayController will automatically reorder your content array to match the sort order. When using key names, you may specify the direction of the sort by appending ASC or DESC to the key name. By default sorting is done in ascending order.

For example,

myController.set('orderBy', 'title DESC');
myController.set('orderBy', ['lastName ASC', 'firstName DESC']);

Normally, you should only use this property if you set the content of the controller to an unordered enumerable such as SC.Set or SC.SelectionSet. In this case the orderBy property is required in order for the controller to property order the content for display.

If you set the content to an array, it is usually best to maintain the array in the proper order that you want to display things rather than using this method to order the array since it requires an extra processing step. You can use this orderBy property, however, for displaying smaller arrays of content.

Note that you can only use addObject() to insert new objects into an array that is ordered. You cannot manually reorder or insert new objects into specific locations because the order is managed by this property instead.

If you pass a function, it should be suitable for use in compare().

Instance Method Detail

addObject(object)

Adds an object to the array. If the content is ordered, this will add the object to the end of the content array. The content is not ordered, the location depends on the implementation of the content.

If the source content does not support adding an object, then this method will throw an exception.

Parameters:
object Object
The object to add to the array.
Returns:
SC.ArrayController
The receiver.
arrangedObjects()

Returns an SC.Array object suitable for use in a CollectionView. Depending on how you have your ArrayController configured, this property may be one of several different values.

canAddContent()

Computed property insides whether you can add content. You can add content as long as the controller isEditable and the content is not a single object.

Note that the only way to simply add object to an ArrayController is to use the addObject() or pushObject() methods. All other methods imply reordering and will fail.

canRemoveContent()

Computed property indicates whether or not the array controller can remove content. You can delete content only if the content is not single content and isEditable is YES.

canReorderContent()

Computed property indicates whether you can reorder content. You can reorder content as long a the controller isEditable and the content is a real SC.Array-like object. You cannot reorder content when orderBy is non-null.

hasContent()

Set to YES if the controller has valid content that can be displayed, even an empty array. Returns NO if the content is null or not enumerable and allowsSingleContent is NO.

indexOf(object, startAt)
Parameters:
object
startAt
length()
Compute the length of the array based on the observable content
propertyWillChange(key)
Parameters:
key
removeObject(object)

Removes the passed object from the array. If the underlying content is a single object, then this simply sets the content to null. Otherwise it will call removeObject() on the content.

Also, if destroyOnRemoval is YES, this will actually destroy the object.

Parameters:
object Object
the object to remove
Returns:
SC.ArrayController
receiver
status()

Returns the current status property for the content. If the content does not have a status property, returns SC.Record.READY.

toString()
Documentation generated by JsDoc Toolkit 2.4.0 on Wed Apr 08 2015 10:02:20 GMT-0600 (CST)