-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservices.js
More file actions
129 lines (103 loc) · 3.06 KB
/
services.js
File metadata and controls
129 lines (103 loc) · 3.06 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
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
/* # StoredObject Class
* Handles storage of objects in the brower's LocalStorage.
* Works with an ORM-like API.
*/
class StoredObject {
// keyname at which all objects will be stored under
static keyName() { return this.name }
// constructor:
// * sets all properties on given parameter as properties of this object;
// * sets object's id;
// * sets object's timestamp.
constructor (params){
if(!params.id){
this.id = (new Date()).valueOf()
this.createdAt = new Date()
}
for(let property in params){
this[property] = params[property]
}
}
// fetches list of all objects, straight from LocalStorage
static fetch(){
return JSON.parse(localStorage.getItem(this.keyName())) || []
}
// stores the list given to LocalStorage
static store(objects){
localStorage.setItem(this.keyName(), JSON.stringify(objects))
}
// fetches list of all objects, then return them as new Object()
static list(){
return this.fetch().map( (item) => new this(item) )
}
// stores an object:
// * if there's no item with this ID yet, appends to the end of the current
// objects list then saves it.
// * if theres an item w/ this ID, overwrites it in the list, then saves it.
// returns the saved object.
save (){
const id = this.id
const objects = this.constructor.fetch()
const index = objects.findIndex( (item) => item.id == id )
if(index != -1){
objects[index] = this
} else {
objects.push(this)
}
this.constructor.store(objects)
return this.constructor.get(id)
}
// fetches a specific object, by id
static get (id) {
const objects = this.fetch()
return new this(objects.find( (item) => item.id == id ))
}
// returns an StoredObject whose id is saved at the PARAMId property.
// ORM-like behavior for relationships.
object (klass){
const fieldName = `${klass.name.toLowerCase()}Id`
return klass.get(this[fieldName])
}
}
/* # Notes service
*/
app.factory('Note', ['Patient', '$filter', function(Patient, $filter){
class Note extends StoredObject {
// returs this note's Patient
patient(){
return this.object(Patient)
}
// overrides list() so that all Note objects listed
// already have their Patients pre-fetched.
static list(){
return super.list().map((item) => {
item.patient = item.patient()
return item
})
}
// returns a properly formated Note timestamp.
get timestamp(){
return $filter('date')(this.createdAt, 'yy-MM-dd, H:mm')
}
}
return Note
}])
/* # Patients service
*/
app.factory('Patient', ['$injector', function($injector){
class Patient extends StoredObject {
// returns this patient's notes
// uses depency injector, which isn't great.
notes(){
const notes = $injector.get('Note').list()
const id = this.id
return notes.filter((item) => item.patientId == id)
}
// property: number of Notes this patient has
// uses this.notes()
get numNotes(){
return this.notes().length
}
}
return Patient
}])