This repository was archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathObject.js
103 lines (90 loc) · 2.93 KB
/
Object.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
var module = angular.module('App');
module.factory('BaseObject', ($q) => {
class BaseObject {
constructor(data) {
Object.assign(this, data);
}
/**
* Switch to high-memory mode
*
* Usually means it's being rendered on screen and should get extra details and keep them updated
*/
open() {}
/**
* Switch to low-memory mode
*
* Unsubscribe unnecessary overhead
*/
close() {}
/**
* Prevents parallel queries and provides a stateful flag for view rendering
*
* Also allows you to permanently store cache to prevent any subsequent calls
*
* @param {string} name Name of property flag to use for caching the promise
* @param {function} callback Executes the query and returns a promise
* @param {boolean} [permanent] If the cache should be removed upon completion (default:false)
*/
cache(name, callback, permanent = false) {
// if promise already present (in progress) then return it instead
if (this[name])
return this[name];
// sets (truthy) flag reference to promise + avoids redundant calls
return this[name] = callback()
// flag cleanup (doesn't affect chaining)
.finally( () => {
if (!permanent)
this[name] = null;
});
}
/**
* object.save() - Convenience wrapper
*
* Sets an `object.saving` flag to the promise (truthy) and clears the flag when finished
* Using `Promise.finally()` allows you to execute code on success OR fail withought affecting chaining
*/
save() {
return this.cache('saving', () => this.id ? this.update() : this.create() );
}
/**
* object.create() - stubbed with example state flag creating
*
* @note Use object.save() instead of calling this method directly
*/
create() {
return this.cache('creating', () => $q.when(this) );
}
/**
* object.update() - stubbed with example state flag updating
*
* @note Use object.save() instead of calling this method directly
*/
update() {
return this.cache('updating', () => $q.when(this) );
}
/**
* object.delete() - stubbed with example state flag updating
*/
delete() {
return this.cache('deleting', () => $q.when(this) );
}
/**
* Make a copy of object for use in forms
*
* When you edit a record in a form, you want the original to be preserved while the user makes changes
* This allows you to edit a record exactly as if you were creating one without having to worry about
* rolling back changes to the object.
*
* @note This makes a shallow copy only
*/
clone() {
var constructor = Object.getPrototypeOf(this).constructor;
return new constructor(this);
}
/**
* Cleans up listeners (should run when discarding object)
*/
destroy() {}
}
return BaseObject;
});