Skip to content

Commit

Permalink
Removed readOnly feature and added some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Onar Vikingstad committed Oct 23, 2009
1 parent 0690580 commit e73e83c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 48 deletions.
22 changes: 7 additions & 15 deletions frameworks/datastore/models/record.js
Expand Up @@ -290,22 +290,14 @@ SC.Record = SC.Object.extend(
recordAttr = this[key],
attrs;

// if read only attribute do not mark as dirty and do not
// make editable
if(recordAttr && recordAttr.get('readOnly')===YES) {
attrs = store.readDataHash(storeKey);
attrs = store.readEditableDataHash(storeKey);
if (!attrs) throw SC.Record.BAD_STATE_ERROR;

// if value is the same, do not flag record as dirty
if (value !== attrs[key]) {
if(!ignoreDidChange) this.beginEditing();
attrs[key] = value;
}
else {
attrs = store.readEditableDataHash(storeKey);
if (!attrs) throw SC.Record.BAD_STATE_ERROR;

// if value is the same, do not flag record as dirty
if (value !== attrs[key]) {
if(!ignoreDidChange) this.beginEditing();
attrs[key] = value;
if(!ignoreDidChange) this.endEditing(key);
}
if(!ignoreDidChange) this.endEditing(key);
}

// if value is primaryKey of record, write it to idsByStoreKey
Expand Down
22 changes: 9 additions & 13 deletions frameworks/datastore/models/record_attribute.js
Expand Up @@ -13,6 +13,15 @@ sc_require('models/record');
generate computed properties on records that can automatically convert data
types and verify data.
When defining an attribute on an SC.Record, you can configure it this way:
{{{
title: SC.Record.attr(String, {
defaultValue: 'Untitled',
isRequired: YES|NO
})
}}}
In addition to having predefined transform types, there is also a way to
set a computed relationship on an attribute. A typical example of this would
be if you have record with a parentGuid attribute, but are not able to
Expand Down Expand Up @@ -94,19 +103,6 @@ SC.RecordAttribute = SC.Object.extend(
*/
useIsoDate: YES,

/**
If YES, setting a new value on this property will not mark the record as
dirty. This will also ensure the datahash does not become locked or in
editable state.
This is useful if your server API returns certain properties
as read only, but you'd still like to set those values client side without
affecting the record state.
@property {Boolean}
*/
readOnly: NO,

// ..........................................................
// HELPER PROPERTIES
//
Expand Down
22 changes: 2 additions & 20 deletions frameworks/datastore/tests/models/record_attribute.js
Expand Up @@ -18,12 +18,12 @@ module("SC.RecordAttribute core methods", {
MyApp.Foo = SC.Record.extend({

// test simple reading of a pass-through prop
firstName: SC.Record.attr(String, { readOnly: YES }),
firstName: SC.Record.attr(String),

// test mapping to another internal key
otherName: SC.Record.attr(String, { key: "firstName" }),

// test mapping Date and readOnly
// test mapping Date
date: SC.Record.attr(Date),
nonIsoDate: SC.Record.attr(Date, { useIsoDate: false }),

Expand Down Expand Up @@ -176,21 +176,3 @@ test("writing a date should generate an ISO date" ,function() {
equals(rec.set('date', date), rec, 'returns reciever');
equals(rec.readAttribute('date'), '2009-04-01T22:28:03-07:00', 'should have new time (%@)'.fmt(date.toString()));
});

test("writing a value to a readOnly attribute should not alter state or make it editable nor locked", function() {

equals(rec.get('firstName'), 'John', 'precond - should return John');

rec.set('firstName', 'Adam');
equals(rec.get('firstName'), 'Adam', 'newly written value should exist');

// now check if it is still ready
equals(rec.get('status'), SC.Record.READY_CLEAN, 'still ready clean state');

// and not editable
equals(MyApp.store.editables.indexOf(rec.get('storeKey')), -1, 'is not editable');

// and not locked
equals(MyApp.store.locks, null, 'is not locked');

});

0 comments on commit e73e83c

Please sign in to comment.