-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
ext.js
332 lines (269 loc) · 8.83 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
330
331
332
import Store from 'ember-data/system/store';
import Model from 'ember-data/system/model';
import InternalModel from 'ember-data/system/model/internal-model';
import JSONSerializer from 'ember-data/serializers/json-serializer';
import FragmentRootState from './states';
import {
internalModelFor,
default as Fragment
} from './fragment';
import FragmentArray from './array/fragment';
/**
@module ember-data-model-fragments
*/
var keys = Object.keys || Ember.keys;
var create = Object.create || Ember.create;
/**
@class Store
@namespace DS
*/
Store.reopen({
/**
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: function(modelName, props) {
var type = this.modelFor(modelName);
Ember.assert("The '" + type + "' model must be a subclass of MF.Fragment", Fragment.detect(type));
var internalModel = new InternalModel(type, null, this, this.container);
// Re-wire the internal model to use the fragment state machine
internalModel.currentState = FragmentRootState.empty;
internalModel._name = null;
internalModel._owner = null;
internalModel.loadedData();
var fragment = internalModel.getRecord();
if (props) {
fragment.setProperties(props);
}
// Add brand to reduce usages of `instanceof`
fragment._isFragment = true;
return fragment;
},
/**
Changes serializer fallbacks for fragments to use `serializer:-fragment`
if registered, then uses the default serializer.
@method serializerFor
@private
@param {String} modelName the record to serialize
@return {DS.Serializer}
*/
serializerFor: function(modelOrClass) {
var modelName;
if (typeof modelOrClass === 'string') {
modelName = modelOrClass;
} else {
modelName = modelOrClass.modelName;
}
// Don't fail on non-model lookups ('application', '-default', etc.)
var type = this.modelFactoryFor(modelName);
// For fragments, don't use the application serializer or adapter default
// as a fallbacks
if (type && Fragment.detect(type)) {
var fallbacks = [
'-fragment',
'-default'
];
return this.lookupSerializer(modelName, fallbacks);
}
return this._super(modelOrClass);
}
});
/**
@class Model
@namespace DS
*/
Model.reopen({
/**
Returns an object, whose keys are changed properties, and value is
an [oldProp, newProp] array. When the model has fragments that have
changed, the property value is simply `true`.
Example
```javascript
App.Mascot = DS.Model.extend({
type: DS.attr('string'),
name: MF.fragment('name')
});
App.Name = DS.Model.extend({
first : DS.attr('string'),
last : DS.attr('string')
});
var person = store.createRecord('person');
person.changedAttributes(); // {}
person.get('name').set('first', 'Tomster');
person.set('type', 'Hamster');
person.changedAttributes(); // { name: true, type: [undefined, 'Hamster'] }
```
@method changedAttributes
@return {Object} an object, whose keys are changed properties,
and value is an [oldProp, newProp] array.
*/
changedAttributes: function() {
var diffData = this._super();
var internalModel = internalModelFor(this);
keys(internalModel._fragments).forEach(function(name) {
// An actual diff of the fragment or fragment array is outside the scope
// of this method, so just indicate that there is a change instead
if (name in internalModel._attributes) {
diffData[name] = true;
}
}, this);
return diffData;
},
willDestroy: function() {
this._super.apply(this, arguments);
var internalModel = internalModelFor(this);
var key, fragment;
// destroy the current state
for (key in internalModel._fragments) {
if (fragment = internalModel._fragments[key]) {
fragment.destroy();
}
}
// destroy the original state
for (key in internalModel._data) {
if (fragment = internalModel._data[key]) {
if (fragment instanceof Fragment || fragment instanceof FragmentArray) {
fragment.destroy();
}
}
}
}
});
// 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) {
var originalFn = obj[name];
obj[name] = function() {
var value = originalFn.apply(this, arguments);
return fn.call(this, value, arguments);
};
}
var InternalModelPrototype = InternalModel.prototype;
/**
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) {
var attrs = snapshot._attributes;
keys(attrs).forEach(function(key) {
var 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;
});
/**
If the model `hasDirtyAttributes` this function will discard any unsaved
changes, recursively doing the same for all fragment properties.
Example
```javascript
record.get('name'); // 'Untitled Document'
record.set('name', 'Doc 1');
record.get('name'); // 'Doc 1'
record.rollbackAttributes();
record.get('name'); // 'Untitled Document'
```
@method rollbackAttributes
*/
decorateMethod(InternalModelPrototype, 'rollbackAttributes', function rollbackFragments() {
for (var key in this._fragments) {
if (this._fragments[key]) {
this._fragments[key].rollbackAttributes();
}
}
});
/**
Before saving a record, its attributes must be moved to in-flight, which must
happen for all fragments as well
@method flushChangedAttributes
*/
decorateMethod(InternalModelPrototype, 'flushChangedAttributes', function flushChangedAttributesFragments() {
var fragment;
// Notify fragments that the record was committed
for (var key in this._fragments) {
if (fragment = this._fragments[key]) {
fragment._flushChangedAttributes();
}
}
});
/**
If the adapter did not return a hash in response to a commit,
merge the changed attributes and relationships into the existing
saved data and notify all fragments of the commit.
@method adapterDidCommit
*/
decorateMethod(InternalModelPrototype, 'adapterDidCommit', function adapterDidCommitFragments(returnValue, args) {
var attributes = (args[0] && args[0].attributes) || create(null);
var fragment;
// Notify fragments that the record was committed
for (var key in this._fragments) {
if (fragment = this._fragments[key]) {
fragment._adapterDidCommit(attributes[key]);
}
}
});
decorateMethod(InternalModelPrototype, 'adapterDidError', function adapterDidErrorFragments(returnValue, args) {
var error = args[0] || create(null);
var fragment;
// Notify fragments that the record was committed
for (var key in this._fragments) {
if (fragment = this._fragments[key]) {
fragment._adapterDidError(error);
}
}
});
/**
@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: function(attributeType) {
if (attributeType.indexOf('-mf-') === 0) {
return getFragmentTransform(this.container, this.store, attributeType);
}
return this._super.apply(this, arguments);
}
});
// Retrieve or create a transform for the specific fragment type
function getFragmentTransform(container, store, attributeType) {
var registry = container._registry || container.registry || container;
var containerKey = 'transform:' + attributeType;
var match = attributeType.match(/^-mf-(fragment|fragment-array|array)(?:\$([^$]+))?(?:\$(.+))?$/);
var transformName = match[1];
var transformType = match[2];
var polymorphicTypeProp = match[3];
if (!registry.has(containerKey)) {
var transformClass = container.lookupFactory('transform:' + transformName);
registry.register(containerKey, transformClass.extend({
store: store,
type: transformType,
polymorphicTypeProp: polymorphicTypeProp
}));
}
return container.lookup(containerKey);
}
export { Store, Model, JSONSerializer };