forked from gaperton/Type-R
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmodel.js
More file actions
33 lines (27 loc) · 989 Bytes
/
model.js
File metadata and controls
33 lines (27 loc) · 989 Bytes
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
// Data objects are defined in nestedtypes package.
import { Model, Collection, define, type } from '@type-r/models'
@define class Checklist extends Collection {
get checked(){ return this.every( item => item.checked ); }
set checked( checked ){
if( checked !== this.checked ){
this.updateEach( item => { item.checked = checked } );
}
}
}
@define
export class ChecklistItem extends Model {
static Collection = Checklist;
static attributes = { // <- Here's an attribute spec. Think of it as a type spec.
name : String,
created : Date,
checked : type( Boolean ).watcher( 'onCheckedChange' ),
subitems : type( Checklist ).watcher( 'onSubitemsChange' )
};
onCheckedChange( checked ){ this.subitems.checked = checked; }
onSubitemsChange( subitems ){
if( subitems.length ){
this.checked = this.subitems.checked;
}
}
remove(){ this.collection.remove( this ); }
}