-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkendo.data.kinvey.js
307 lines (280 loc) · 12.6 KB
/
kendo.data.kinvey.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
/**
* kinvey-kendo-data-source - Kinvey extension of the Kendo UI for jQuery DataSource component.
* @version v0.0.1
* @author Kinvey, Inc.
* @link https://www.kinvey.com
* @license Apache-2.0
*/
(function () {
"use strict";
var ERROR_MESSAGES = {
"NoKinveyInstanceMessage": "An instance of the Kinvey JavaScript SDK is not available. Initialize the Kinvey global object.",
"NoDataStoreCollectionNameMessage": "A collection name or a data store instance must be provided to the data source options.",
"BatchCreateNotSupportedMessage": "Batch create is not supported. Set the data source 'batch' option to 'false'.",
"BatchUpdateNotSupportedMessage": "Batch update is not supported. Set the data source 'batch' option to 'false'.",
"BatchDeleteNotSupportedMessage": "Batch delete is not supported. Set the data source 'batch' option to 'false'."
};
var KINVEY_DATA_SOURCE_CONSTANTS = {
idField: "_id"
};
if ((typeof window !== typeof undefined && typeof window.jQuery === typeof undefined) || (typeof window.kendo === typeof undefined || !window.kendo.data)) {
return;
}
var $ = window.jQuery,
kendo = window.kendo,
extend = $.extend,
each = $.each,
isArray = Array.isArray,
total = null;
var kinveyTransport = kendo.data.RemoteTransport.extend({
init: function (options) {
if (!Kinvey) {
throw new Error(ERROR_MESSAGES.NoKinveyInstanceMessage);
}
if (!options.typeName && !options.dataStore) {
throw new Error(ERROR_MESSAGES.NoDataStoreCollectionNameMessage);
}
this.dataStore = options.dataStore ? options.dataStore : Kinvey.DataStore.collection(options.typeName, Kinvey.DataStoreType.Network);
kendo.data.RemoteTransport.fn.init.call(this, options);
},
read: function (readOptions) {
var methodOption = this.options.read;
var self = this;
if (methodOption && methodOption.url) {
return kendo.data.RemoteTransport.fn.read.call(this, readOptions);
}
var queryOptions = translateKendoQueryToKinvey(readOptions.data);
var query = new Kinvey.Query(queryOptions);
var isRequestingServerPaging = queryOptions.limit >= 0;
if (isRequestingServerPaging) {
var countQueryOptions = {};
if (queryOptions.filter) {
countQueryOptions.filter = queryOptions.filter;
}
var countQuery = new Kinvey.Query(countQueryOptions);
self.dataStore.count(countQuery).toPromise().then(function (totalItemsCount) {
total = totalItemsCount;
return self.dataStore.find(query).toPromise();
}).then(function onSuccess(entities) {
readOptions.success(entities);
}).catch(function onErr(readError) {
readOptions.error(readError); // e.error(response, status, error);
});
} else {
self.dataStore.find(query).toPromise().
then(function onSuccess(entities) {
readOptions.success(entities);
}).catch(function onErr(err) {
readOptions.error(err);
});
}
},
update: function (options) {
var methodOption = this.options.update;
if (methodOption && methodOption.url) {
return kendo.data.RemoteTransport.fn.read.call(this, options);
}
var isMultiple = isArray(options.data.models);
if (isMultiple) {
var batchUpdateUnsupportedErrorMessage = ERROR_MESSAGES.BatchUpdateNotSupportedMessage;
options.error(batchUpdateUnsupportedErrorMessage);
} else {
var itemForUpdate = options.data;
this.dataStore.save(itemForUpdate).
then(function onSuccess(updateResult) {
options.success(updateResult);
}).catch(function onErr(updateErr) {
options.error(updateErr);
});
}
},
create: function (options) {
var methodOption = this.options.create;
if (methodOption && methodOption.url) {
return kendo.data.RemoteTransport.fn.read.call(this, options);
}
var isMultiple = isArray(options.data.models);
if (isMultiple) {
var batchCreateUnsupportedErrorMessage = ERROR_MESSAGES.BatchCreateNotSupportedMessage;
options.error(batchCreateUnsupportedErrorMessage);
} else {
var createData = options.data;
if (createData._id === "") {
delete createData._id;
}
this.dataStore.save(createData).then(function onSuccess(createResult) {
options.success(createResult);
}).catch(function onErr(createError) {
options.error(createError);
});
}
},
destroy: function (options) {
var methodOption = this.options.destroy;
if (methodOption && methodOption.url) {
return kendo.data.RemoteTransport.fn.read.call(this, options);
}
var methodHeaders;
if (methodOption && methodOption.headers) {
methodHeaders = methodOption.headers;
}
var isMultiple = isArray(options.data.models);
if (isMultiple) {
var batchDeleteUnsupportedErrorMessage = ERROR_MESSAGES.BatchDeleteNotSupportedMessage;
options.error(batchDeleteUnsupportedErrorMessage);
}
this.dataStore.removeById(options.data._id)
.then(function onSuccess(destroyResult) {
options.success(destroyResult);
}).catch(function onErr(destroyError) {
options.error(destroyError);
});
},
parameterMap: function (options, type) {
var result = kendo.data.transports.kinvey.parameterMap(options, type, true);
return result;
}
});
extend(true, kendo.data, {
transports: {
kinvey: kinveyTransport
},
schemas: {
kinvey: {
type: "json",
total: function (data) {
return total ? total : data.length;
},
data: function (data) {
return data;
},
model: {
id: KINVEY_DATA_SOURCE_CONSTANTS.idField
}
}
}
});
function translateKendoQueryToKinvey(data) {
var result = {};
if (data) {
if (data.skip || data.skip === 0) {
result.skip = data.skip;
delete data.skip;
}
if (data.take) {
result.limit = data.take;
delete data.take;
// we only need the "limit" and "skip" modifiers, "page" and "pageSize" are not required on the server
delete data.pageSize;
delete data.page;
}
if (data.sort) {
var sortExpressions = data.sort;
var sort = {};
if (!isArray(sortExpressions)) {
sortExpressions = [sortExpressions];
}
each(sortExpressions, function (idx, value) {
sort[value.field] = value.dir === 'asc' ? 1 : -1;
});
result.sort = sort;
delete data.sort;
}
if (data.filter) {
var filterOptions = data.filter;
var kinveyFilterOptions = {};
var logicalOperator = filterOptions.logic;
var kendoFiltersArray = filterOptions.filters;
var kinveyFiltersArray = [];
var i, loopCount = kendoFiltersArray.length,
currentKendoFilter,
currentKendoFilterOperator,
currentKendoFilterFieldName,
currentKendoFilterValue,
currentKinveyFilter;
for (i = 0; i < loopCount; i++) {
currentKendoFilter = kendoFiltersArray[i];
currentKendoFilterOperator = currentKendoFilter["operator"];
currentKendoFilterFieldName = currentKendoFilter.field;
currentKendoFilterValue = currentKendoFilter.value;
currentKinveyFilter = {};
switch (currentKendoFilterOperator) {
case "eq":
currentKinveyFilter[currentKendoFilterFieldName] = currentKendoFilterValue;
break;
case "neq":
currentKinveyFilter[currentKendoFilterFieldName] = {
"$ne": currentKendoFilterValue
};
break;
case "isnull":
currentKinveyFilter[currentKendoFilterFieldName] = {
"$eq": null
};
break;
case "isnotnull":
currentKinveyFilter[currentKendoFilterFieldName] = {
"$ne": null
};
break;
case "lt":
currentKinveyFilter[currentKendoFilterFieldName] = {
"$lt": currentKendoFilterValue
};
break;
case "gt":
currentKinveyFilter[currentKendoFilterFieldName] = {
"$gt": currentKendoFilterValue
};
break;
case "lte":
currentKinveyFilter[currentKendoFilterFieldName] = {
"$lte": currentKendoFilterValue
};
break;
case "gte":
currentKinveyFilter[currentKendoFilterFieldName] = {
"$gte": currentKendoFilterValue
};
break;
case "startswith":
currentKinveyFilter[currentKendoFilterFieldName] = {
"$regex": "^" + currentKendoFilterValue
};
break;
case "endswith":
currentKinveyFilter[currentKendoFilterFieldName] = {
"$regex": currentKendoFilterValue + "$"
};
break;
case "isin":
if (!isArray(currentKendoFilterValue)) {
currentKendoFilterValue = [currentKendoFilterValue]
}
currentKinveyFilter[currentKendoFilterFieldName] = {
"$in": currentKendoFilterValue
};
break;
case "isnotin":
if (!isArray(currentKendoFilterValue)) {
currentKendoFilterValue = [currentKendoFilterValue]
}
currentKinveyFilter[currentKendoFilterFieldName] = {
"$nin": currentKendoFilterValue
};
break;
default:
throw new Error("Unsupported filtering operator: " + currentKendoFilterOperator);
}
kinveyFiltersArray.push(currentKinveyFilter);
}
var kinveyFilterOptions = {};
var kinveyLogicalOperator = logicalOperator === "and" ? "$and" : "$or";
kinveyFilterOptions[kinveyLogicalOperator] = kinveyFiltersArray;
delete data.filter;
result.filter = kinveyFilterOptions;
}
}
return result;
}
}());