-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfrom.ts
More file actions
69 lines (54 loc) · 2.31 KB
/
from.ts
File metadata and controls
69 lines (54 loc) · 2.31 KB
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
import { AnyType, ChainableAttributeSpec, Record } from '../record';
import { CollectionReference, parseReference } from './commons';
/********
* Reference to model by id.
*
* Untyped attribute. Holds model id, when unresolved. When resolved, is substituted
* with a real model.
*
* No model changes are detected and counted as owner's change. That's intentional.
*/
/** @private */
type RecordRefValue = Record | string;
/** @private */
class RecordRefType extends AnyType {
// It is always serialized as an id, whenever it's resolved or not.
toJSON( value : RecordRefValue ){
return value && typeof value === 'object' ? value.id : value;
}
// Wne
clone( value : RecordRefValue ){
return value && typeof value === 'object' ? value.id : value;
}
// Model refs by id are equal when their ids are equal.
isChanged( a : RecordRefValue, b : RecordRefValue){
var aId = a && ( (<Record>a).id == null ? a : (<Record>a).id ),
bId = b && ( (<Record>b).id == null ? b : (<Record>b).id );
return aId !== bId;
}
// Refs are always valid.
validate( model, value, name ){}
}
export function memberOf<R extends typeof Record>( this : void, masterCollection : CollectionReference, T? : R ) : ChainableAttributeSpec<R> {
const getMasterCollection = parseReference( masterCollection );
const typeSpec = new ChainableAttributeSpec<R>({
value : null,
_metatype : RecordRefType
});
return typeSpec
.get( function( objOrId : RecordRefValue, name : string ) : Record {
if( typeof objOrId === 'object' ) return objOrId;
// So, we're dealing with an id reference. Resolve it.
const collection = getMasterCollection( this );
let record : Record = null;
// If master collection exists and is not empty...
if( collection && collection.length ){
// Silently update attribute with record from this collection.
record = collection.get( objOrId ) || null;
this.attributes[ name ] = record;
// Subscribe for events manually. delegateEvents won't be invoked.
record && this._attributes[ name ].handleChange( record, null, this, {} );
}
return record;
});
}