forked from blikblum/nextbone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputed.js
167 lines (144 loc) · 4 KB
/
computed.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
import { isEmpty, reduce, omit } from 'lodash-es';
const computeFieldValue = (computedField, model) => {
if (computedField && computedField.get) {
const values = getDependentValues(computedField.depends, model);
return computedField.get.call(model, values);
}
};
const getDependentValues = (depends, model) => {
if (!depends) return {};
return depends.reduce((memo, field) => {
if (typeof field === 'string') {
memo[field] = model.get(field);
}
return memo;
}, {});
};
const createFieldFromArray = arr => {
const depends = [];
let get, set;
arr.forEach(item => {
switch (typeof item) {
case 'string':
depends.push(item);
break;
case 'function':
if (!get) {
get = item;
} else {
set = item;
}
break;
default:
break;
}
});
return { depends, get, set };
};
const createNormalizedOptions = options => {
if (!options) return;
const excludeFromJSON = reduce(
options,
(result, def, key) => {
if (def.toJSON !== true) {
result.push(key);
}
return result;
},
[]
);
const fields = [];
for (let key in options) {
const field = options[key];
if (Array.isArray(field)) {
fields.push({ name: key, field: createFieldFromArray(field) });
} else if (field && (field.set || field.get)) {
fields.push({ name: key, field: field });
}
}
return { excludeFromJSON, fields };
};
class ComputedFields {
constructor(model, fields) {
this.model = model;
this._bindModelEvents(fields);
}
_bindModelEvents(fields) {
fields.forEach(computedField => {
const fieldName = computedField.name;
const field = computedField.field;
const updateComputed = () => {
var value = computeFieldValue(field, this.model);
this.model.set(fieldName, value, { __computedSkip: true });
};
const updateDependent = (model, value, options) => {
if (options && options.__computedSkip) {
return;
}
if (field.set) {
const values = getDependentValues(field.depends, this.model);
value = value || this.model.get(fieldName);
field.set.call(this.model, value, values);
this.model.set(values, options);
}
};
this.model.on('change:' + fieldName, updateDependent);
if (field.depends) {
field.depends.forEach(dependent => {
if (typeof dependent === 'string') {
this.model.on('change:' + dependent, updateComputed);
}
if (typeof dependent === 'function') {
dependent.call(this.model, updateComputed);
}
});
}
if (!isEmpty(this.model.attributes)) {
updateComputed();
}
});
}
}
const excludeFromJSONKey = Symbol('excludeFromJSON');
const getComputedOptions = ctor => {
if (ctor.hasOwnProperty('__computedOptions')) {
return ctor.__computedOptions;
}
return (ctor.__computedOptions = createNormalizedOptions(ctor.computed));
};
const createClass = ModelClass => {
return class extends ModelClass {
constructor(...args) {
super(...args);
const options = getComputedOptions(this.constructor);
if (options) {
this.computedFields = new ComputedFields(this, options.fields);
if (options.excludeFromJSON.length) {
this[excludeFromJSONKey] = options.excludeFromJSON;
}
}
}
toJSON(...args) {
const result = super.toJSON(...args);
const excludeFromJSON = this[excludeFromJSONKey];
if (!excludeFromJSON || (args[0] && args[0].computed)) {
return result;
}
return omit(result, excludeFromJSON);
}
};
};
const withComputed = ctorOrDescriptor => {
if (typeof ctorOrDescriptor === 'function') {
return createClass(ctorOrDescriptor);
}
const { kind, elements } = ctorOrDescriptor;
return {
kind,
elements,
finisher(ctor) {
return createClass(ctor);
}
};
};
export { withComputed };