-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathadapter.js
208 lines (165 loc) · 5.86 KB
/
adapter.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
(function() {
var get = Ember.get;
DS.DjangoRESTSerializer = DS.RESTSerializer.extend({
patchInJSONRoot: function(json, type, many) {
var pJSON, root;
root = this.rootForType(type);
if (many === true) {
root = this.pluralize(root);
}
pJSON = {};
pJSON[root] = json;
return pJSON;
},
keyForHasMany: function(type, name) {
return this.keyForAttributeName(type, name);
},
keyForBelongsTo: function(type, name) {
return this.keyForAttributeName(type, name);
},
extract: function(loader, json, type, records) {
json = this.patchInJSONRoot(json, type, false);
this._super(loader, json, type, records);
},
extractMany: function(loader, json, type, records) {
json = this.patchInJSONRoot(json, type, true);
this._super(loader, json, type, records);
}
});
})();
(function() {
function rejectionHandler(reason) {
Ember.Logger.error(reason, reason.message);
throw reason;
}
var get = Ember.get, set = Ember.set;
DS.DjangoRESTAdapter = DS.RESTAdapter.extend({
bulkCommit: false,
serializer: DS.DjangoRESTSerializer,
createRecord: function(store, type, record) {
var root, adapter, data;
root = this.rootForType(type);
adapter = this;
data = this.serialize(record);
return this.ajax(this.buildURL(root), "POST", {
data: data
}).then(function(json){
adapter.didCreateRecord(store, type, record, json);
}, function(xhr) {
adapter.didError(store, type, record, xhr);
throw xhr;
}).then(null, rejectionHandler);
},
updateRecord: function(store, type, record) {
var id, root, adapter, data;
id = get(record, 'id');
root = this.rootForType(type);
adapter = this;
data = this.serialize(record);
return this.ajax(this.buildURL(root, id), "PUT", {
data: data
}).then(function(json){
adapter.didUpdateRecord(store, type, record, json);
}, function(xhr) {
adapter.didError(store, type, record, xhr);
throw xhr;
}).then(null, rejectionHandler);
},
findMany: function(store, type, ids, parent) {
var adapter, root, url;
adapter = this;
if (parent) {
url = this.buildFindManyUrlWithParent(type, parent);
} else {
root = this.rootForType(type);
url = this.buildURL(root);
}
return this.ajax(url, "GET", {
}).then(function(json) {
adapter.didFindMany(store, type, json);
}).then(null, rejectionHandler);
},
ajax: function(url, type, hash) {
hash = hash || {};
hash.cache = false;
return this._super(url, type, hash);
},
buildURL: function(record, suffix) {
var url = this._super(record, suffix);
if (url.charAt(url.length -1) !== '/') {
url += '/';
}
return url;
},
buildFindManyUrlWithParent: function(type, parent) {
var root, url, endpoint, parentType, parentValue;
endpoint = parent.get('findManyKey');
parentType = parent.get('findManyType');
if (typeof endpoint !== 'string') {
parent.eachRelationship(function(name, relationship) {
if (relationship.kind === 'hasMany' && relationship.type === type) {
endpoint = relationship.key;
parentType = relationship.parentType;
}
});
}
Ember.assert("could not find a relationship for the specified child type", typeof endpoint !== "undefined");
endpoint = this.serializer.keyForAttributeName(parentType, endpoint);
parentValue = parent.get('id');
root = this.rootForType(parentType);
url = this.buildURL(root, parentValue);
return url + endpoint + '/';
},
/**
RESTAdapter expects HTTP 422 for invalid records and a JSON response
with errors inside JSON root `errors`, however DRF uses 400
and errors without a JSON root.
*/
didError: function(store, type, record, xhr) {
if (xhr.status === 400) {
var data = JSON.parse(xhr.responseText);
var errors = {};
// Convert error key names
record.eachAttribute(function(name) {
var attr = this.serializer.keyForAttributeName(type, name);
if (attr in data) {
errors[name] = data[attr];
}
}, this);
record.eachRelationship(function(name, relationship) {
var attr = null;
if (relationship.kind === 'belongsTo') {
attr = this.serializer.keyForBelongsTo(type, name);
} else {
attr = this.serializer.keyForHasMany(type, name);
}
if (attr in data) {
errors[name] = data[attr];
}
}, this);
store.recordWasInvalid(record, errors);
} else {
this._super.apply(this, arguments);
}
}
});
})();
(function() {
DS.DjangoRESTStore = DS.Store.extend({
findMany: function(type, idsOrReferencesOrOpaque, record, relationship) {
var ret;
// check for hasMany relationship
if (typeof relationship === 'object' && relationship.kind === 'hasMany') {
record.set('findManyKey', relationship.key);
record.set('findManyType', relationship.parentType);
}
ret = this._super(type, idsOrReferencesOrOpaque, record, relationship);
// clear the variables we set to be clean
record.set('findManyKey', null);
record.set('findManyType', null);
return ret;
}
});
})();
(function() {
})();