-
Notifications
You must be signed in to change notification settings - Fork 291
/
record_attribute.js
471 lines (382 loc) · 14.6 KB
/
record_attribute.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// ==========================================================================
// Project: SproutCore - JavaScript Application Framework
// Copyright: ©2006-2009 Sprout Systems, Inc. and contributors.
// Portions ©2008-2009 Apple Inc. All rights reserved.
// License: Licened under MIT license (see license.js)
// ==========================================================================
sc_require('models/record');
/** @class
A RecordAttribute describes a single attribute on a record. It is used to
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
determine which record type to map to before looking at the guid (or any
other attributes). To set up such a computed property, you can attach a
function in the attribute definition of the SC.Record subclass:
{{{
relatedToComputed: SC.Record.toOne(function() {
return (this.readAttribute('relatedToComputed').indexOf("foo")==0) ? MyApp.Foo : MyApp.Bar;
})
}}}
Notice that we are not using .get() to avoid another transform which would
trigger an infinite loop.
You usually will not work with RecordAttribute objects directly, though you
may extend the class in any way that you like to create a custom attribute.
A number of default RecordAttribute types are defined on the SC.Record.
@extends SC.Object
@since SproutCore 1.0
*/
SC.RecordAttribute = SC.Object.extend(
/** @scope SC.RecordAttribute.prototype */ {
/**
The default value. If attribute is null or undefined, this default value
will be substituted instead. Note that defaultValues are not converted
so the value should be in the output type expected by the attribute.
@property {Object}
*/
defaultValue: null,
/**
The attribute type. Must be either an object class or a property path
naming a class. The built in handler allows all native types to pass
through, converts records to ids and dates to UTF strings.
If you use the attr() helper method to create a RecordAttribute instance,
it will set this property to the first parameter you pass.
@property {Object|String}
*/
type: String,
/**
The underlying attribute key name this attribute should manage. If this
property is left empty, then the key will be whatever property name this
attribute assigned to on the record. If you need to provide some kind
of alternate mapping, this provides you a way to override it.
@property {String}
*/
key: null,
/**
If YES, then the attribute is required and will fail validation unless
the property is set to a non-null or undefined value.
@property {Boolean}
*/
isRequired: NO,
/**
If NO then attempts to edit the attribute will be ignored.
@property {Boolean}
*/
isEditable: YES,
/**
If set when using the Date format, expect the ISO8601 date format.
This is the default.
@property {Boolean}
*/
useIsoDate: YES,
// ..........................................................
// HELPER PROPERTIES
//
/**
Returns the type, resolved to a class. If the type property is a regular
class, returns the type unchanged. Otherwise attempts to lookup the
type as a property path.
@property {Object}
*/
typeClass: function() {
var ret = this.get('type');
if (SC.typeOf(ret) === SC.T_STRING) ret = SC.objectForPropertyPath(ret);
return ret ;
}.property('type').cacheable(),
/**
Finds the transform handler.
@property {Function}
*/
transform: function() {
var klass = this.get('typeClass') || String,
transforms = SC.RecordAttribute.transforms,
ret ;
// walk up class hierarchy looking for a transform handler
while(klass && !(ret = transforms[SC.guidFor(klass)])) {
// check if super has create property to detect SC.Object's
if(klass.superclass.hasOwnProperty('create')) klass = klass.superclass ;
// otherwise return the function transform handler
else klass = SC.T_FUNCTION ;
}
return ret ;
}.property('typeClass').cacheable(),
// ..........................................................
// LOW-LEVEL METHODS
//
/**
Converts the passed value into the core attribute value. This will apply
any format transforms. You can install standard transforms by adding to
the SC.RecordAttribute.transforms hash. See
SC.RecordAttribute.registerTransform() for more.
@param {SC.Record} record the record instance
@param {String} key the key used to access this attribute on the record
@param {Object} value the property value
@returns {Object} attribute value
*/
toType: function(record, key, value) {
var transform = this.get('transform'),
type = this.get('typeClass');
if (transform && transform.to) {
value = transform.to(value, this, type, record, key) ;
}
return value ;
},
/**
Converts the passed value from the core attribute value. This will apply
any format transforms. You can install standard transforms by adding to
the SC.RecordAttribute.transforms hash. See
SC.RecordAttribute.registerTransform() for more.
@param {SC.Record} record the record instance
@param {String} key the key used to access this attribute on the record
@param {Object} value the property value
@returns {Object} attribute value
*/
fromType: function(record, key, value) {
var transform = this.get('transform'),
type = this.get('typeClass');
if (transform && transform.from) {
value = transform.from(value, this, type, record, key);
}
return value;
},
/**
The core handler. Called from the property.
@param {SC.Record} record the record instance
@param {String} key the key used to access this attribute on the record
@param {Object} value the property value if called as a setter
@returns {Object} property value
*/
call: function(record, key, value) {
var attrKey = this.get('key') || key, nvalue;
if (value !== undefined) {
// careful: don't overwrite value here. we want the return value to
// cache.
nvalue = this.fromType(record, key, value) ; // convert to attribute.
record.writeAttribute(attrKey, nvalue);
} else {
value = record.readAttribute(attrKey);
if (SC.none(value) && (value = this.get('defaultValue'))) {
if (typeof value === SC.T_FUNCTION) {
value = this.defaultValue(record, key, this);
// write default value so it doesn't have to be executed again
if(record.attributes()) record.writeAttribute(attrKey, value, true);
}
} else value = this.toType(record, key, value);
}
return value ;
},
// ..........................................................
// INTERNAL SUPPORT
//
/** @private - Make this look like a property so that get() will call it. */
isProperty: YES,
/** @private - Make this look cacheable */
isCacheable: YES,
/** @private - needed for KVO property() support */
dependentKeys: [],
/** @private */
init: function() {
sc_super();
// setup some internal properties needed for KVO - faking 'cacheable'
this.cacheKey = "__cache__" + SC.guidFor(this) ;
this.lastSetValueKey = "__lastValue__" + SC.guidFor(this) ;
}
}) ;
// ..........................................................
// CLASS METHODS
//
/**
The default method used to create a record attribute instance. Unlike
create(), takes an attributeType as the first parameter which will be set
on the attribute itself. You can pass a string naming a class or a class
itself.
@param {Object|String} attributeType the assumed attribute type
@param {Hash} opts optional additional config options
@returns {SC.RecordAttribute} new instance
*/
SC.RecordAttribute.attr = function(attributeType, opts) {
if (!opts) opts = {} ;
if (!opts.type) opts.type = attributeType || String ;
return this.create(opts);
};
/** @private
Hash of registered transforms by class guid.
*/
SC.RecordAttribute.transforms = {};
/**
Call to register a transform handler for a specific type of object. The
object you pass can be of any type as long as it responds to the following
methods:
| *to(value, attr, klass, record, key)* | converts the passed value (which will be of the class expected by the attribute) into the underlying attribute value |
| *from(value, attr, klass, record, key)* | converts the underyling attribute value into a value of the class |
@param {Object} klass the type of object you convert
@param {Object} transform the transform object
@returns {SC.RecordAttribute} receiver
*/
SC.RecordAttribute.registerTransform = function(klass, transform) {
SC.RecordAttribute.transforms[SC.guidFor(klass)] = transform;
};
// ..........................................................
// STANDARD ATTRIBUTE TRANSFORMS
//
// Object, String, Number just pass through.
/** @private - generic converter for Boolean records */
SC.RecordAttribute.registerTransform(Boolean, {
/** @private - convert an arbitrary object value to a boolean */
to: function(obj) {
return SC.none(obj) ? null : !!obj;
}
});
/** @private - generic converter for Numbers */
SC.RecordAttribute.registerTransform(Number, {
/** @private - convert an arbitrary object value to a Number */
to: function(obj) {
return SC.none(obj) ? null : Number(obj) ;
}
});
/** @private - generic converter for Strings */
SC.RecordAttribute.registerTransform(String, {
/** @private -
convert an arbitrary object value to a String
allow null through as that will be checked separately
*/
to: function(obj) {
if (!(typeof obj === SC.T_STRING) && !SC.none(obj) && obj.toString) {
obj = obj.toString();
}
return obj;
}
});
/** @private - generic converter for Array */
SC.RecordAttribute.registerTransform(Array, {
/** @private - check if obj is an array
*/
to: function(obj) {
if (!SC.isArray(obj) && !SC.none(obj)) {
obj = [];
}
return obj;
}
});
/** @private - generic converter for Object */
SC.RecordAttribute.registerTransform(Object, {
/** @private - check if obj is an object */
to: function(obj) {
if (!(typeof obj === 'object') && !SC.none(obj)) {
obj = {};
}
return obj;
}
});
/** @private - generic converter for SC.Record-type records */
SC.RecordAttribute.registerTransform(SC.Record, {
/** @private - convert a record id to a record instance */
to: function(id, attr, recordType, parentRecord) {
var store = parentRecord.get('store');
if (SC.none(id) || (id==="")) return null;
else return store.find(recordType, id);
},
/** @private - convert a record instance to a record id */
from: function(record) { return record ? record.get('id') : null; }
});
/** @private - generic converter for transforming computed record attributes */
SC.RecordAttribute.registerTransform(SC.T_FUNCTION, {
/** @private - convert a record id to a record instance */
to: function(id, attr, recordType, parentRecord) {
recordType = recordType.apply(parentRecord);
var store = parentRecord.get('store');
return store.find(recordType, id);
},
/** @private - convert a record instance to a record id */
from: function(record) { return record.get('id'); }
});
/** @private - generic converter for Date records */
SC.RecordAttribute.registerTransform(Date, {
/** @private - convert a string to a Date */
to: function(str, attr) {
var ret ;
if (attr.get('useIsoDate')) {
var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
"(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\\.([0-9]+))?)?" +
"(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?",
d = str.toString().match(new RegExp(regexp)),
offset = 0,
date = new Date(d[1], 0, 1),
time ;
if (d[3]) { date.setMonth(d[3] - 1); }
if (d[5]) { date.setDate(d[5]); }
if (d[7]) { date.setHours(d[7]); }
if (d[8]) { date.setMinutes(d[8]); }
if (d[10]) { date.setSeconds(d[10]); }
if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
if (d[14]) {
offset = (Number(d[16]) * 60) + Number(d[17]);
offset *= ((d[15] == '-') ? 1 : -1);
}
offset -= date.getTimezoneOffset();
time = (Number(date) + (offset * 60 * 1000));
ret = new Date();
ret.setTime(Number(time));
} else ret = new Date(Date.parse(str));
return ret ;
},
_dates: {},
_zeropad: function(num) {
return ((num<0) ? '-' : '') + ((num<10) ? '0' : '') + Math.abs(num);
},
/** @private - convert a date to a string */
from: function(date) {
var ret = this._dates[date.getTime()];
if (ret) return ret ;
// figure timezone
var zp = this._zeropad,
tz = 0-date.getTimezoneOffset()/60;
tz = (tz === 0) ? 'Z' : '%@:00'.fmt(zp(tz));
this._dates[date.getTime()] = ret = "%@-%@-%@T%@:%@:%@%@".fmt(
zp(date.getFullYear()),
zp(date.getMonth()+1),
zp(date.getDate()),
zp(date.getHours()),
zp(date.getMinutes()),
zp(date.getSeconds()),
tz) ;
return ret ;
}
});
if (SC.DateTime && !SC.RecordAttribute.transforms[SC.guidFor(SC.DateTime)]) {
/**
Registers a transform to allow SC.DateTime to be used as a record attribute,
ie SC.Record.attr(SC.DateTime);
Because SC.RecordAttribute is in the datastore framework and SC.DateTime in
the foundation framework, and we don't know which framework is being loaded
first, this chunck of code is duplicated in both frameworks.
IF YOU EDIT THIS CODE MAKE SURE YOU COPY YOUR CHANGES to record_attribute.js.
*/
SC.RecordAttribute.registerTransform(SC.DateTime, {
/** @private
Convert a String to a DateTime
*/
to: function(str, attr) {
if (SC.none(str)) return str;
var format = attr.get('format');
return SC.DateTime.parse(str, format ? format : SC.DateTime.recordFormat);
},
/** @private
Convert a DateTime to a String
*/
from: function(dt, attr) {
if (SC.none(dt)) return dt;
var format = attr.get('format');
return dt.toFormattedString(format ? format : SC.DateTime.recordFormat);
}
});
}