-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
stateful.js
196 lines (160 loc) · 4.26 KB
/
stateful.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
import { compare } from '@ember/utils';
import ArrayProxy from '@ember/array/proxy';
import { makeArray, A } from '@ember/array';
import { copy, Copyable } from 'ember-copy';
import { get, set, computed } from '@ember/object';
import { fragmentDidDirty, fragmentDidReset } from '../states';
/**
@module ember-data-model-fragments
*/
/**
A state-aware array that is tied to an attribute of a `DS.Model` instance.
@class StatefulArray
@namespace MF
@extends Ember.ArrayProxy
*/
const StatefulArray = ArrayProxy.extend(Copyable, {
/**
A reference to the array's owner record.
@property owner
@type {DS.Model}
*/
owner: null,
/**
The array's property name on the owner record.
@property name
@private
@type {String}
*/
name: null,
init() {
this._super(...arguments);
this._pendingData = undefined;
set(this, '_originalState', []);
},
content: computed(function() {
return A();
}),
/**
Copies the array by calling copy on each of its members.
@method copy
@return {array} a new array
*/
copy() {
return this.map(copy);
},
/**
@method setupData
@private
@param {Object} data
*/
setupData(data) {
// Since replacing the contents of the array can trigger changes to fragment
// array properties, this method can get invoked recursively with the same
// data, so short circuit here once it's been setup the first time
if (this._pendingData === data) {
return;
}
this._pendingData = data;
let processedData = this._normalizeData(makeArray(data));
let content = get(this, 'content');
// This data is canonical, so create rollback point
set(this, '_originalState', processedData);
// Completely replace the contents with the new data
content.replace(0, get(content, 'length'), processedData);
this._pendingData = undefined;
},
/**
@method _normalizeData
@private
@param {Object} data
*/
_normalizeData(data) {
return data;
},
/**
@method _createSnapshot
@private
*/
_createSnapshot() {
// Since elements are not models, a snapshot is simply a mapping of raw values
return this.toArray();
},
/**
@method _flushChangedAttributes
*/
_flushChangedAttributes() {},
/**
@method _adapterDidCommit
@private
*/
_adapterDidCommit(data) {
if (data) {
this.setupData(data);
} else {
// Fragment array has been persisted; use the current state as the original state
set(this, '_originalState', this.toArray());
}
},
_adapterDidError(/* error */) {
// No-Op
},
/**
If this property is `true` the contents of the array do not match its
original state. The array has local changes that have not yet been saved by
the adapter. This includes additions, removals, and reordering of elements.
Example
```javascript
array.toArray(); // [ 'Tom', 'Yehuda' ]
array.get('isDirty'); // false
array.popObject(); // 'Yehuda'
array.get('isDirty'); // true
```
@property hasDirtyAttributes
@type {Boolean}
@readOnly
*/
hasDirtyAttributes: computed('[]', '_originalState', function() {
return compare(this.toArray(), get(this, '_originalState')) !== 0;
}),
/**
This method reverts local changes of the array's contents to its original
state.
Example
```javascript
array.toArray(); // [ 'Tom', 'Yehuda' ]
array.popObject(); // 'Yehuda'
array.toArray(); // [ 'Tom' ]
array.rollbackAttributes();
array.toArray(); // [ 'Tom', 'Yehuda' ]
```
@method rollbackAttributes
*/
rollbackAttributes() {
this.setObjects(get(this, '_originalState'));
},
/**
Method alias for `toArray`.
@method serialize
@return {Array}
*/
serialize() {
return this.toArray();
},
arrayContentDidChange() {
this._super(...arguments);
let record = get(this, 'owner');
let key = get(this, 'name');
// Any change to the size of the fragment array means a potential state change
if (get(this, 'hasDirtyAttributes')) {
fragmentDidDirty(record, key, this);
} else {
fragmentDidReset(record, key);
}
},
toStringExtension() {
let ownerId = get(this, 'owner.id');
return `owner(${ownerId})`;
}
});
export default StatefulArray;