| 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 |
1×
66×
1×
158×
158×
158×
123×
35×
158×
66×
66×
66×
154×
5×
2×
2×
2×
67×
58×
3×
1×
1×
1×
2×
6×
14×
2×
12×
52×
2×
50×
86×
86×
50×
50×
50×
86×
34×
29×
29×
34×
6×
6×
6×
6×
6×
3×
3×
3×
3×
4×
2×
4×
| import Ember from 'ember';
import InternalObject from './object';
import State from './state';
const {
copy,
Logger: { error }
} = Ember;
const isKeyUnderscored = key => key && key.indexOf('_') === 0;
const replace = (from, to, json) => {
let value = json[from];
delete json[from];
if(value === undefined) {
delete json[to];
} else {
json[to] = value;
}
return json;
};
export default class InternalDocument extends InternalObject {
constructor(store, database) {
super(store, null);
this.database = database;
this.state = new State();
}
get isDocument() {
return true;
}
get isNew() {
return this.state.isNew;
}
get isDeleted() {
return this.state.isDeleted;
}
_didDestroyModel() {
super._didDestroyModel();
this.database._didDestroyModelForInternalDocument(this);
}
_createModel() {
return this.store._createDocumentModel(this);
}
getId() {
return this._getValueNotify('_id', 'model');
}
setId(id) {
if(!this.isNew && id !== this.getId()) {
let current = this.getId();
error(`Document id cannot be changed after document is saved. Attempted to set id '${id}' for document '${current}'`);
return current;
}
return this._setValueNotify('_id', id, 'model');
}
getRev() {
return this._getValueNotify('_rev', 'model');
}
setValue(key) {
if(isKeyUnderscored(key)) {
return;
}
return super.setValue(...arguments);
}
getValue(key) {
if(isKeyUnderscored(key)) {
return;
}
return super.getValue(...arguments);
}
willDeserialize(json, type) {
json = json || {};
if(type === 'model') {
json = copy(json, false);
replace('id', '_id', json);
replace('rev', '_rev', json);
}
return json;
}
didSerialize(json, type) {
if(type === 'model') {
replace('_id', 'id', json);
replace('_rev', 'rev', json);
}
return json;
}
deserializeDeleted(json, changed) {
let type = 'document';
json = this.willDeserialize(json, type);
let { id, rev } = json;
this._setValue('_id', id, type, changed);
this._setValue('_rev', rev, type, changed);
}
deserializeSaved(json, changed) {
let type = 'document';
let { id, rev } = json;
this._setValue('_id', id, type, changed);
this._setValue('_rev', rev, type, changed);
}
//
enqueueSave() {
return this.database._enqueueInternalSave(this, ...arguments);
}
enqueueLoad() {
return this.database._enqueueInternalLoad(this, ...arguments);
}
enqueueReload() {
return this.database._enqueueInternalReload(this, ...arguments);
}
enqueueDelete() {
return this.database._enqueueInternalDelete(this, ...arguments);
}
}
|