Class: SC.ObjectController


Extends SC.Controller.

An ObjectController gives you a simple way to manage the editing state of an object. You can use an ObjectController instance as a "proxy" for your model objects.

Any properties you get or set on the object controller, will be passed through to its content object. This allows you to setup bindings to your object controller one time for all of your views and then swap out the content as needed.

Working with Arrays

An ObjectController can accept both arrays and single objects as content. If the content is an array, the ObjectController will do its best to treat the array as a single object. For example, if you set the content of an ObjectController to an array of Contact records and then call:

contactController.get('name');

The controller will check the name property of each Contact in the array. If the value of the property for each Contact is the same, that value will be returned. If the any values are different, then an array will be returned with the values from each Contact in them.

Most SproutCore views can work with both arrays and single content, which means that most of the time, you can simply hook up your views and this will work.

If you would prefer to make sure that your ObjectController is always working with a single object and you are using bindings, you can always setup your bindings so that they will convert the content to a single object like so:

contentBinding: SC.Binding.single('MyApp.listController.selection') ;

This will ensure that your content property is always a single object instead of an array.

Defined in: object.js

Since:
SproutCore 1.0

Field Summary

Instance Methods

Field Detail

allowsMultipleContent Boolean

If YES, then setting the content to an enumerable or an array with more than one item will cause the Controller to attempt to treat the array as a single object. Use of get(), for example, will get every property on the enumerable and return it. set() will set the property on every item in the enumerable.

If NO, then setting content to an enumerable with multiple items will be treated like setting a null value. hasContent will be NO.

content Object

Set to the object you want this controller to manage. The object should usually be a single value; not an array or enumerable. If you do supply an array or enumerable with a single item in it, the ObjectController will manage that single item.

Usually your content object should implement the SC.Observable mixin, but this is not required. All SC.Object-based objects support SC.Observable

isEditable Boolean

Makes a controller editable or not editable. The SC.Controller class itself does not do anything with this property but subclasses will respect it when modifying content.

Instance Method Detail

contentPropertyDidChange(target, key)

Invoked whenever any property on the content object changes.

The default implementation will simply notify any observers that the property has changed. You can override this method if you need to do some custom work when the content property changes.

If you have set the content property to an enumerable with multiple objects and you set allowsMultipleContent to YES, this method will be called anytime any property in the set changes.

If all properties have changed on the content or if the content itself has changed, this method will be called with a key of "*".

Parameters:
target Object
the content object
key String
the property that changes
Returns:
void
destroy()

Override this method to destroy the selected object.

The default just passes this call onto the content object if it supports it, and then sets the content to null.

Unlike most calls to destroy() this will not actually destroy the controller itself; only the the content. You continue to use the controller by setting the content to a new value.

Returns:
SC.ObjectController
receiver
hasContent()

Becomes YES whenever this object is managing content. Usually this means the content property contains a single object or an array or enumerable with a single item. Array's or enumerables with multiple items will normally make this property NO unless allowsMultipleContent is YES.

observableContent()

Primarily for internal use. Normally you should not access this property directly.

Returns the actual observable object proxied by this controller. Usually this property will mirror the content property. In some cases - notably when setting content to an enumerable, this may return a different object.

Note that if you set the content to an enumerable which itself contains enumerables and allowsMultipleContent is NO, this will become null.

toString()
unknownProperty(key, value)

Called whenver you try to get/set an unknown property. The default implementation will pass through to the underlying content object but you can override this method to do some other kind of processing if needed.

Parameters:
key String
key being retrieved
value Object
value to set or undefined if reading only
Returns:
Object
property value
Documentation generated by JsDoc Toolkit 2.4.0 on Wed Apr 08 2015 10:02:21 GMT-0600 (CST)