/
ember-data-dreamfactory-adapter.js
201 lines (167 loc) · 5.74 KB
/
ember-data-dreamfactory-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
/*
* namespace for the DSP Adapter
*/
var EmberDreamFactoryAdapter = {};
/*
* Serializer overrides
*/
EmberDreamFactoryAdapter.Serializer = DS.RESTSerializer.extend({
isNewSerializerAPI: true,
normalizeResponse: function(store, modelClass, payload, id, requestType) {
var debug = true;
console.group("normalizeResponse");
console.log("store, modelClass, payload, id, requestType", store, modelClass, payload, id, requestType)
var namespacedPayload = {};
if( id ) {
if( payload.record ) {
namespacedPayload[modelClass.modelName] = payload.record[0];
} else {
namespacedPayload[modelClass.modelName] = payload;
}
} else {
namespacedPayload[Ember.String.pluralize(modelClass.modelName)] = payload.record;
}
if( debug ) { console.log("namespacedPayload", namespacedPayload); }
var response = this._super(store, modelClass, namespacedPayload, id, requestType);
if( debug ) { console.log("response", response); }
console.groupEnd();
return response;
},
typeForRoot: function (key) {
return Ember.String.dasherize(Ember.String.singularize(key));
},
serialize: function (record, options) {
options = options || { includeId: true };
return this._super(record, options);
},
// set the JSON root to 'record' to make DSP happy
serializeIntoHash: function (data, type, record, options) {
var root = 'record';
data[root] = this.serialize(record, options);
}
});
/*
* An Ember Data Adapter written to use DreamFactory REST API
* @type {DS.RESTAdapter}
*/
EmberDreamFactoryAdapter.Adapter = DS.RESTAdapter.extend({
defaultSerializer: '-dreamfactory',
init: function () {
this._super();
this.set('headers', {
'X-DreamFactory-Application-Name': this.get('applicationName')
});
},
query: function (store, type, query) {
if (typeof query.procedure !== 'undefined') {
// A stored procedure was requested. Alter buildUrl to use the correct API path
var proc = query.procedure;
var data = {};
var httpRequestType = 'GET'; // default request to GET
var adapter = this;
// remove the procedure property from the query object
delete query.procedure;
// Test to see if the query has additional properties. If so we need to change the httpRequestType to a HTTP POST
if (!this.isEmpty(query)) {
httpRequestType = 'POST';
}
// the serializer expects all results to be wrapped in a 'record' object so we add that here
query.wrapper = 'record';
return new Ember.RSVP.Promise(function(resolve, reject) {
adapter.ajax(adapter.buildProcUrl(proc), httpRequestType, { data: query }).then(function(json) {
resolve(json);
}, function(reason) {
reject(reason.responseJSON);
});
});
} else {
// normal call so we use the normal findQuery
return this._super(store, type, query);
}
},
// Used https://github.com/clintjhill/ember-parse-adapter/blob/master/dist/ember-parse-adapter.js#L290 as a starting point
createRecord: function(store, type, record) {
var data = {};
var serializer = store.serializerFor(type.modelName);
serializer.serializeIntoHash(data, type, record);
var adapter = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
adapter.ajax(adapter.buildURL(type.modelName, id), "POST", { data: data }).then(function(json){
// if the request is a success we'll return the same data we passed in
var completed = Ember.merge(data.record, json);
resolve(completed);
}, function(reason){
reject(reason.responseJSON);
});
});
},
// Used https://github.com/clintjhill/ember-parse-adapter/blob/master/dist/ember-parse-adapter.js#L290 as a starting point
updateRecord: function(store, type, record) {
var data = {};
var serializer = store.serializerFor(type.modelName);
serializer.serializeIntoHash(data, type, record);
var adapter = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
// hack to make DSP send back the full object
adapter.ajax(adapter.buildURL(type.modelName) + '?fields=*', "PUT", { data: data }).then(function(json){
// if the request is a success we'll return the same data we passed in
resolve(json);
}, function(reason){
reject(reason.responseJSON);
});
});
},
pathForType: function (type) {
return Ember.String.pluralize(Ember.String.capitalize(Ember.String.camelize(type)));
},
buildProcUrl: function (proc) {
var url = [],
host = this.get('host'),
prefix = this.urlPrefix();
url.push('_proc');
url.push(proc);
if (prefix) { url.unshift(prefix); }
url = url.join('/');
return url;
},
// tests for an empty object
isEmpty: function (obj) {
// null and undefined are "empty"
if (obj == null) return true;
// Assume if it has a length property with a non-zero value
// that that property is correct.
if (obj.length > 0) return false;
if (obj.length === 0) return true;
// Otherwise, does it have any properties of its own?
// Note that this doesn't handle
// toString and valueOf enumeration bugs in IE < 9
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) return false;
}
return true;
},
sessionToken: Ember.computed('headers.X-DreamFactory-Session-Token', {
get(key) {
return this.get('headers.X-DreamFactory-Session-Token');
},
set(key, value) {
this.set('headers.X-DreamFactory-Session-Token', value);
return value;
}
})
});
EmberDreamFactoryAdapter.setupContainer = function(container) {
container.register('serializer:-dreamfactory', EmberDreamFactoryAdapter.Serializer);
};
/**
* Setup the DSP Adapter in the app.
*/
Ember.onLoad('Ember.Application', function(Application) {
Application.initializer({
after: "ember-data",
name: 'dreamfactory-adapter',
initialize: function(container, application) {
EmberDreamFactoryAdapter.setupContainer(container);
}
});
});