-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
ext.js
329 lines (271 loc) · 9.02 KB
/
ext.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import { assert } from '@ember/debug';
import Store from '@ember-data/store';
import Model from '@ember-data/model';
// eslint-disable-next-line ember/use-ember-data-rfc-395-imports
import { coerceId, RecordData, InternalModel, normalizeModelName } from 'ember-data/-private';
import JSONSerializer from '@ember-data/serializer/json';
import FragmentRootState from './states';
import FragmentRecordData from './record-data';
import {
internalModelFor,
default as Fragment
} from './fragment';
import { isPresent } from '@ember/utils';
import { computed } from '@ember/object';
import { getOwner } from '@ember/application';
import { assign } from '@ember/polyfills';
import { lte, gte } from 'ember-compatibility-helpers';
function serializerForFragment(owner, normalizedModelName) {
let serializer = owner.lookup(`serializer:${normalizedModelName}`);
if (serializer !== undefined) {
return serializer;
}
// no serializer found for the specific model, fallback and check for application serializer
serializer = owner.lookup('serializer:-fragment');
if (serializer !== undefined) {
return serializer;
}
// final fallback, no model specific serializer, no application serializer, no
// `serializer` property on store: use json-api serializer
serializer = owner.lookup('serializer:-default');
return serializer;
}
/**
@module ember-data-model-fragments
*/
const InternalModelPrototype = InternalModel.prototype;
const RecordDataPrototype = RecordData.prototype;
assign(RecordDataPrototype, {
eachFragmentKey(fn) {
this._fragments = this._fragments || Object.create({});
Object.keys(this._fragments).forEach(fn);
},
eachFragmentKeyValue(fn) {
this.eachFragmentKey((key) => {
const value = this.getFragment(key);
if (value) {
fn(key, value);
}
});
},
getOwner() {
return this._owner;
},
setOwner(value) {
this._owner = value;
},
setName(value) {
this._name = value;
},
getName() {
return this._name;
},
getFragment(name) {
this._fragments = this._fragments || Object.create({});
return this._fragments[name];
},
setFragment(name, fragment) {
this._fragments = this._fragments || Object.create({});
this._fragments[name] = fragment;
return this._fragments[name];
},
didCommit(data) {
this._isNew = false;
if (data) {
if (data.relationships) {
this._setupRelationships(data);
}
if (data.id) {
// didCommit provided an ID, notify the store of it
this.storeWrapper.setRecordId(this.modelName, data.id, this.clientId);
this.id = coerceId(data.id);
}
data = data.attributes;
// Notify fragments that the record was committed
this.eachFragmentKeyValue((key, fragment) => fragment._didCommit(data[key]));
} else {
this.eachFragmentKeyValue((key, fragment) => fragment._didCommit());
}
const changedKeys = this._changedKeys(data);
assign(this._data, this.__inFlightAttributes, this._attributes, data);
this._attributes = null;
this._inFlightAttributes = null;
this._updateChangedAttributes();
return changedKeys;
}
});
/**
@class Store
@namespace DS
*/
Store.reopen({
createRecordDataFor(type, id, lid, storeWrapper) {
let identifier;
if (lte('ember-data', '3.13.0')) {
throw new Error('This version of Ember Data Model Fragments is incompatible with Ember Data Versions below 3.13. See matrix at https://github.com/lytics/ember-data-model-fragments#compatibility for details.');
}
if (gte('ember-data', '3.15.0')) {
identifier = this.identifierCache.getOrCreateRecordIdentifier({ type, id, lid });
} else {
identifier = { type, id, clientId: lid };
}
return new FragmentRecordData(identifier, storeWrapper);
},
/**
Create a new fragment that does not yet have an owner record.
The properties passed to this method are set on the newly created
fragment.
To create a new instance of the `name` fragment:
```js
store.createFragment('name', {
first: 'Alex',
last: 'Routé'
});
```
@method createRecord
@param {String} type
@param {Object} properties a hash of properties to set on the
newly created fragment.
@return {MF.Fragment} fragment
*/
createFragment(modelName, props) {
assert(`The '${modelName}' model must be a subclass of MF.Fragment`, this.isFragment(modelName));
let internalModel;
if (gte('ember-data', '3.15.0')) {
const identifier = this.identifierCache.createIdentifierForNewRecord({ type: modelName });
internalModel = this._internalModelForResource(identifier);
} else {
let identifier = { type: modelName, id: `${ Math.random()}`, lid: `${ Math.random()}` };
internalModel = this._internalModelForResource(identifier);
}
// Re-wire the internal model to use the fragment state machine
internalModel.currentState = FragmentRootState.empty;
internalModel._recordData._name = null;
internalModel._recordData._owner = null;
internalModel.loadedData();
let fragment = internalModel.getRecord();
if (props) {
fragment.setProperties(props);
}
// invoke the ready callback ( to mimic DS.Model behaviour )
fragment.trigger('ready');
// Add brand to reduce usages of `instanceof`
fragment._isFragment = true;
return fragment;
},
/**
Returns true if the modelName is a fragment, false if not
@method isFragment
@private
@param {String} the modelName to check if a fragment
@return {boolean}
*/
isFragment(modelName) {
if (modelName === 'application' || modelName === '-default') {
return false;
}
let type = this.modelFor(modelName);
return Fragment.detect(type);
},
serializerFor(modelName) {
// this assertion is cargo-culted from ember-data TODO: update comment
assert('You need to pass a model name to the store\'s serializerFor method', isPresent(modelName));
assert(`Passing classes to store.serializerFor has been removed. Please pass a dasherized string instead of ${modelName}`, typeof modelName === 'string');
let owner = getOwner(this);
let normalizedModelName = normalizeModelName(modelName);
if (this.isFragment(normalizedModelName)) {
return serializerForFragment(owner, normalizedModelName);
} else {
return this._super(...arguments);
}
}
});
/**
@class Model
@namespace DS
*/
Model.reopen({
willDestroy() {
this._super(...arguments);
let internalModel = internalModelFor(this);
// destroy the current state
internalModel._recordData.resetFragments();
}
});
Model.reopenClass({
fields: computed(function() {
let map = new Map();
this.eachComputedProperty((name, meta) => {
if (meta.isFragment) {
map.set(name, 'fragment');
} else if (meta.isRelationship) {
map.set(name, meta.kind);
} else if (meta.isAttribute) {
map.set(name, 'attribute');
}
});
return map;
}).readOnly()
});
// Replace a method on an object with a new one that calls the original and then
// invokes a function with the result
function decorateMethod(obj, name, fn) {
let originalFn = obj[name];
obj[name] = function() {
let value = originalFn.apply(this, arguments);
return fn.call(this, value, arguments);
};
}
/**
Override parent method to snapshot fragment attributes before they are
passed to the `DS.Model#serialize`.
@method _createSnapshot
@private
*/
decorateMethod(InternalModelPrototype, 'createSnapshot', function createFragmentSnapshot(snapshot) {
let attrs = snapshot._attributes;
Object.keys(attrs).forEach((key) => {
let attr = attrs[key];
// If the attribute has a `_createSnapshot` method, invoke it before the
// snapshot gets passed to the serializer
if (attr && typeof attr._createSnapshot === 'function') {
attrs[key] = attr._createSnapshot();
}
});
return snapshot;
});
/**
@class JSONSerializer
@namespace DS
*/
JSONSerializer.reopen({
/**
Enables fragment properties to have custom transforms based on the fragment
type, so that deserialization does not have to happen on the fly
@method transformFor
@private
*/
transformFor(attributeType) {
if (attributeType.indexOf('-mf-') !== 0) {
return this._super(...arguments);
}
const owner = getOwner(this);
const containerKey = `transform:${attributeType}`;
if (!owner.hasRegistration(containerKey)) {
const match = attributeType.match(/^-mf-(fragment|fragment-array|array)(?:\$([^$]+))?(?:\$(.+))?$/);
const transformName = match[1];
const type = match[2];
const polymorphicTypeProp = match[3];
let transformClass = owner.factoryFor(`transform:${transformName}`);
transformClass = transformClass && transformClass.class;
transformClass = transformClass.extend({
type,
polymorphicTypeProp,
store: this.store
});
owner.register(containerKey, transformClass);
}
return owner.lookup(containerKey);
}
});
export { Store, Model, JSONSerializer };