Class SC.Record

Extends: SC.Object
Defined in: frameworks/sproutcore/models/record.js
Availability: since SproutCore 1.0

A Record is the core model class in SproutCore. It is analogous to NSManagedObject in Core Data and EOEnterpriseObject in the Enterprise Objects Framework (aka WebObjects), or ActiveRecord::Base in Rails.

To create a new model class, in your SproutCore workspace, do:

$ sc-gen model my_app/my_model

This will create MyApp.MyModel in clients/my_app/models/my_model.js.

The core attributes hash is used to store the values of a record in a format that can be easily passed to/from the server. The values should generally be stored in their raw string form. References to external records should be stored as primary keys.

Normally you do not need to work with the attributes hash directly. Instead you should use get/set on normal record properties. If the property is not defined on the object, then the record will check the attributes hash instead.

You can bulk update attributes from the server using the updateAttributes() method.

Property Summary

Properties borrowed from class SC.Object:
isClass, isObject, outlets

Method Summary

Methods borrowed from class SC.Object:
$super, awake, create, createArray, extend, init, instanceOf, invokeLater, keys, kindOf, mixin, objectClassName, outlet, respondsTo, tryToPerform
Methods borrowed from class SC.Observable:
addObserver, allPropertiesDidChange, automaticallyNotifiesObserversFor, beginPropertyChanges, bind, decrementProperty, didChangeFor, endPropertyChanges, get, getEach, getPath, incrementProperty, logProperty, notifyPropertyChange, observeOnce, propertyDidChange, propertyObserver, propertyWillChange, registerDependentKey, set, setIfChanged, setPath, toggleProperty

Property Details

changeCount

Set to non-zero whenever the record has uncommitted changes.

-   Object changeCount

Discussion

dataSource

The item providing the data for this.

-   Object dataSource

Discussion

Set to either the store or a Server. Setting it to the Store will make refresh and commit effectively null-ops.

destroyURL

The URL where this record can be destroyed.

-   Object destroyURL

Discussion

Usually you would send the value for this URL from the server in response to requests from Sproutcore.

isDeleted

Set to true when the record is deleted.

-   Object isDeleted

Discussion

Will cause it to be removed from any member collections. Once no more objects hold references to it, the property will be disabled.

newRecord

When a new empty record is created, this will be set to true.

-   Object newRecord

Discussion

It will be set to false again the first time the record is committed.

primaryKey

This is the primary key used to distinguish records.

-   Object primaryKey

Discussion

If the keys match, the records are assumed to be identical.

properties

Override this with the properties you want the record to manage.

-   Object properties

Discussion

refreshURL

The URL where this record can be refreshed.

-   Object refreshURL

Discussion

Usually you would send the value for this URL from the server in response to requests from Sproutcore.

resourceURL

Set this URL to point to the type of resource this record is.

-   Object resourceURL

Discussion

If you are using SC.Server, then put a '%@' where you expect the primaryKey to be inserted to identify the record.

updateURL

The URL where this record can be updated.

-   Object updateURL

Discussion

Usually you would send the value for this URL from the server in response to requests from Sproutcore.

Method Details

attributes()

This will return the current set of attributes as a hash you can send back to the server.

-   Object  attributes ()

Return Value

Object - 

Discussion

commit()

Invoked by the UI to tell the model this record should be saved.

-   commit ()

Discussion

Override to support server changes. Note that this is used to support both the create and update components of CRUD.

compareTo()

Compares the receiver to the passed object, using the array of keys to determine the order.

-   Number  compareTo (object, orderBy)

Parameters
object
orderBy

Optional.

Return Value

Number - 

Discussion

You can use this method as part of a call to sort() on an array.

destroy()

This can delete the record.

-   destroy ()

Discussion

The non-server version just sets isDeleted.

matchCondition()

Returns true if the value of key matches the passed value.

-   Boolean  matchCondition (key, value)

Parameters
key
value
Return Value

Boolean - 

Discussion

This is used by matchConditions().

matchConditions()

Used to match records to a set of conditions.

-   Boolean  matchConditions (conditions)

Parameters
conditions
Return Value

Boolean - 

Discussion

By default, this will call matchCondition on each condition.

readAttribute()

Gets an attribute, converting it to the proper format.

-   value  readAttribute (key)

Parameters
string key
Return Value

value - 

Discussion

recordDidChange()

You can invoke this method anytime you need to make the record as dirty and needing a commit to the server.

-   recordDidChange ()

Discussion

refresh()

Invoked by the UI to request the model object be updated from the server.

-   refresh ()

Discussion

Override to actually support server changes.

unknownProperty()

If you try to get/set a property not defined by the record, then this method will be called.

-   Object  unknownProperty (key, value)

Parameters
String key
Object value
Return Value

Object - 

Discussion

It will try to get the value from the set of attributes.

updateAttributes()

This will take the incoming set of attributes and update internal set.

-   Boolean  updateAttributes (newAttrs, replace, isLoaded)

Parameters
Object newAttrs
Boolean replace
Return Value

Boolean - 

Discussion

Note that if the attributes have never been set, then the object you pass in may become the new set of attribute. This assumes the attrs you pass in will not be modified later. This method also assumes it is coming from the server, so the change count will be reset.

updateProperties()

This method should be used by the server to push updated data into a record.

-   void  updateProperties (data, isLoaded)

Parameters
data
isLoaded
Return Value

void - 

Discussion

The data should be a hash with strings and arrays. This will use any types you define to convert the values into their correct type. Note that references to external objects should be a string with the primaryKey value of the record.

writeAttribute()

Updates the attribute, converting it back to the property format.

-   Object  writeAttribute (key, value)

Parameters
String key
Object value
Return Value

Object - 

Discussion
Documentation generated by JsDoc Toolkit 2.0.1 on Tue Aug 12 2008 03:11:20 GMT-0700 (PDT)