| 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.
- (Object) changeCount
- (Object) dataSource
- (Object) destroyURL
- (Object) isDeleted
- (Object) newRecord
- (Object) primaryKey
- (Object) properties
- (Object) refreshURL
- (Object) resourceURL
- (Object) updateURL
-
Object
attributes()
-
commit()
-
Number
compareTo(object, orderBy)
-
destroy()
-
Boolean
matchCondition(key, value)
-
Boolean
matchConditions(conditions)
-
value
readAttribute(key)
-
recordDidChange()
-
refresh()
-
Object
unknownProperty(key, value)
-
Boolean
updateAttributes(newAttrs, replace, isLoaded)
-
void
updateProperties(data, isLoaded)
-
Object
writeAttribute(key, value)
Set to non-zero whenever the record has uncommitted changes.
- Object changeCount
The item providing the data for this.
- Object dataSource
Set to either the store or a Server. Setting it to the Store will make refresh and commit effectively null-ops.
The URL where this record can be destroyed.
- Object destroyURL
Usually you would send the value for this URL from the server in response to requests from Sproutcore.
Set to true when the record is deleted.
- Object isDeleted
Will cause it to be removed from any member collections. Once no more objects hold references to it, the property will be disabled.
When a new empty record is created, this will be set to true.
- Object newRecord
It will be set to false again the first time the record is committed.
This is the primary key used to distinguish records.
- Object primaryKey
If the keys match, the records are assumed to be identical.
Override this with the properties you want the record to manage.
- Object properties
The URL where this record can be refreshed.
- Object refreshURL
Usually you would send the value for this URL from the server in response to requests from Sproutcore.
This will return the current set of attributes as a hash you can send back to the server.
- Object attributes ()
Object -
Invoked by the UI to tell the model this record should be saved.
- commit ()
Override to support server changes. Note that this is used to support both the create and update components of CRUD.
Compares the receiver to the passed object, using the array of keys to determine the order.
- Number compareTo (object, orderBy)
Optional.
Number -
You can use this method as part of a call to sort() on an array.
This can delete the record.
- destroy ()
The non-server version just sets isDeleted.
Returns true if the value of key matches the passed value.
- Boolean matchCondition (key, value)
Boolean -
This is used by matchConditions().
Used to match records to a set of conditions.
- Boolean matchConditions (conditions)
Boolean -
By default, this will call matchCondition on each condition.
Gets an attribute, converting it to the proper format.
- value readAttribute (key)
value -
You can invoke this method anytime you need to make the record as dirty and needing a commit to the server.
- recordDidChange ()
Invoked by the UI to request the model object be updated from the server.
- refresh ()
Override to actually support server changes.
If you try to get/set a property not defined by the record, then this method will be called.
- Object unknownProperty (key, value)
Object -
It will try to get the value from the set of attributes.
This will take the incoming set of attributes and update internal set.
- Boolean updateAttributes (newAttrs, replace, isLoaded)
Boolean -
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.
This method should be used by the server to push updated data into a record.
- void updateProperties (data, isLoaded)
void -
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.