-
Notifications
You must be signed in to change notification settings - Fork 27
/
progress.data.kendo.js
893 lines (812 loc) · 41.6 KB
/
progress.data.kendo.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/*
Copyright (c) 2015-2019 Progress Software Corporation and/or its subsidiaries or affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*global jQuery, kendo, progress*/
/*jslint nomen: true*/
/*jslint vars: false*/
if (typeof (kendo) !== "undefined") {
(function () {
// "use strict";
var JSDODataReader, JSDOTransport, JSDOObservable = new kendo.Observable();
function initializeJSDO(transport, options) {
var jsdo, resourceName;
if (options.jsdo instanceof progress.data.JSDO) {
jsdo = options.jsdo;
} else if (typeof (options.jsdo) === "string") {
// Create a new JSDO instance using the specified configuration
resourceName = options.jsdo;
// Create JSDO
jsdo = new progress.data.JSDO({ name: resourceName });
} else {
throw new Error("JSDO: jsdo property must be either a JSDO instance or a string.");
}
if (transport.tableRef === undefined && jsdo._defaultTableRef) {
transport.tableRef = jsdo._defaultTableRef._name;
}
if (transport.tableRef === undefined) {
throw new Error("JSDO: A tableRef must be specified when using a multi-table DataSet.");
} else if (jsdo[transport.tableRef] === undefined) {
throw new Error("JSDO: tableRef '" + transport.tableRef + "' is not present in JSDO definition.");
}
return jsdo;
}
// This define functions to read the data object from the JSDO DataSource
JSDODataReader = kendo.data.readers.json.extend({
init: function (arg1) {
// init function
var event = {},
transport;
// Query the transport object to obtain a transport reference
JSDOObservable.trigger("info", event);
transport = this.transport = event.sender._events.info.transport;
kendo.data.readers.json.fn.init.call(this, arg1);
// Overrides model property after init.call
// because if model is defined at the object level init.call removes it
this.model = kendo.data.Model.define({
init: function (data) {
var record;
if (!data || jQuery.isEmptyObject(data)) {
data = transport._getInitialValues();
}
record = transport._convertDataTypes(data);
transport.jsdo._deleteProdsProperties(record, true);
kendo.data.Model.fn.init.call(this, record);
},
id: "_id",
fields: transport._getModel()
});
},
total: function (data) {
return data.total || (data.data ? data.data.length : data.length);
},
data: function (data) {
return data.data || data;
}
});
// This define transport for JSDO DataSource providing implementation for:
// create, read, update, destroy, submit
JSDOTransport = kendo.data.RemoteTransport.extend({
init: function (options) {
var transport = this,
fnName;
if (options.tableRef !== undefined) {
transport.tableRef = options.tableRef;
}
transport.jsdo = initializeJSDO(transport, options);
transport._initFromServer = false;
transport.autoSave = options.autoSave !== undefined ? options.autoSave : true;
transport.readLocal = options.readLocal !== undefined ? options.readLocal : false;
transport.countFnName = options.countFnName;
transport.useArrays = options.useArrays !== undefined ? options.useArrays : false;
if (transport.countFnName !== undefined) {
if (typeof (transport.jsdo[transport.countFnName]) !== "function") {
throw new Error("Invoke operation '" +
transport.countFnName + "' for countFnName is not defined.");
}
} else if (transport.jsdo._resource.generic.count !== undefined) {
for (fnName in transport.jsdo._resource.fn) {
if (transport.jsdo._resource.fn.hasOwnProperty(fnName)) {
if (transport.jsdo._resource.generic.count === transport.jsdo._resource.fn[fnName]["function"]) {
transport.countFnName = fnName;
break;
}
}
}
}
// Define "info" event to return transport object to reader
JSDOObservable.one("info", function (e) {
e.sender._events.info.transport = transport;
});
transport._initConvertTypes();
kendo.data.RemoteTransport.fn.init.call(this, options);
},
_initConvertTypes: function () {
// _initConvertTypes:
// Initializes transport._convertTypes to indicate whether a conversion of the data is needed
// when it is passed to Kendo UI.
// This operation is currently only needed for date fields that are stored as strings.
// Sets array _dateFields to the fields of date fields to convert.
var transport = this,
i,
schema,
fieldName,
dateFields = [],
arrayFields = [],
convertDateFields = false;
transport._convertTypes = false;
schema = transport.jsdo[transport.tableRef].getSchema();
for (i = 0; i < schema.length; i += 1) {
fieldName = schema[i].name;
if (fieldName.length > 0 && fieldName.charAt(0) !== "_") {
if (schema[i].type === "string" &&
schema[i].format &&
(schema[i].format.indexOf("date") !== -1)) {
dateFields.push(fieldName);
if (!convertDateFields) {
convertDateFields = true;
}
} else if (!transport.useArrays && schema[i].type === "array") {
arrayFields.push(fieldName);
if (!convertDateFields && schema[i].ablType &&
schema[i].ablType.indexOf("DATE") === 0) {
convertDateFields = true;
}
}
}
}
if (dateFields.length > 0 || arrayFields.length > 0) {
transport._convertTypes = true;
// _convertFields: Object containing arrays for each data type to convert
transport._convertFields = {};
transport._convertFields._arrayFields = [];
transport._convertFields._dateFields = [];
}
if (dateFields.length > 0) {
transport._convertFields._dateFields = dateFields;
}
if (convertDateFields) {
transport._convertFields._datePattern = new RegExp("^([0-9]+)?-([0-9]{2})?-([0-9]{2})?$");
transport._convertFields._dateTimePattern = new RegExp(
"^([0-9]+)?-([0-9]{2})?-([0-9]{2})?" +
"T([0-9]{2})?:([0-9]{2})?:([0-9]{2})?.([0-9]{3})?$"
);
}
if (arrayFields.length > 0) {
transport._convertFields._arrayFields = arrayFields;
}
},
_convertStringToDate: function (data, fieldName, targetFieldName) {
var transport = this,
array,
ablType,
orig;
if (!targetFieldName) {
targetFieldName = fieldName;
}
// Check if string is <year>-<month>-<day>
array = transport._convertFields._datePattern.exec(data[targetFieldName]) || [];
if (array.length > 0) {
data[targetFieldName] = new Date(parseInt(array[1], 10),
parseInt(array[2], 10) - 1,
parseInt(array[3], 10));
} else {
ablType = transport.jsdo[transport.tableRef]._fields[fieldName.toLowerCase()].ablType;
if (ablType === "DATETIME") {
array = transport._convertFields._dateTimePattern.exec(data[targetFieldName]) || [];
if (array.length > 0) {
// Convert date to local time zone
data[targetFieldName] = new Date(parseInt(array[1], 10),
parseInt(array[2], 10) - 1,
parseInt(array[3], 10),
parseInt(array[4], 10),
parseInt(array[5], 10),
parseInt(array[6], 10),
parseInt(array[7], 10));
}
}
// Check to see if it was converted
if (typeof (data[targetFieldName]) === "string") {
orig = data[targetFieldName];
try {
data[targetFieldName] = new Date(data[targetFieldName]);
}
catch (e) {
// Conversion to a date object was not successful
data[targetFieldName] = orig;
console.log(progress.data._getMsgText("jsdoMSG000",
"_convertStringToDate() could not convert to date object: " + orig));
}
}
}
},
_convertDataTypes: function (data) {
// _convertDataTypes:
// Converts data types in the specified data record.
// Data record could come from the JSDO or from the Kendo UI DataSource.
// Returns a reference to the record.
// Returns a copy when useArrays is undefined or false.
var transport = this,
i,
k,
fieldName,
schemaInfo,
prefixElement,
elementName,
copy;
// Remove prods:rowState property from data object
delete data["prods:rowState"];
if (!transport.useArrays && transport._convertTypes && (transport._convertFields._arrayFields.length > 0)) {
copy = {};
transport.jsdo._copyRecord(transport.jsdo._buffers[transport.tableRef], data, copy);
data = copy;
}
if (!transport._convertTypes) {
return data;
}
for (k = 0; k < transport._convertFields._arrayFields.length; k += 1) {
fieldName = transport._convertFields._arrayFields[k];
if (data[fieldName]) {
schemaInfo = transport.jsdo[transport.tableRef]._fields[fieldName.toLowerCase()];
prefixElement = transport.jsdo._getArrayField(fieldName);
for (i = 0; i < schemaInfo.maxItems; i += 1) {
// ABL arrays are 1-based
elementName = prefixElement.name + (i + 1);
if (!transport.jsdo[transport.tableRef]._fields[elementName.toLowerCase()]) {
// Skip element if a field with the same name exists
// Extract value from array field into individual field
// Array is removed later
data[elementName] = data[fieldName][i];
// Convert string DATE fields to JS DATE
if ((schemaInfo.ablType) && (schemaInfo.ablType.indexOf("DATE") === 0) && (typeof (data[elementName]) === "string")) {
transport._convertStringToDate(data, fieldName, elementName);
}
}
}
if (!transport.useArrays) {
delete data[fieldName];
}
}
}
for (k = 0; k < transport._convertFields._dateFields.length; k += 1) {
fieldName = transport._convertFields._dateFields[k];
if (typeof (data[fieldName]) === "string") {
transport._convertStringToDate(data, fieldName);
}
}
return data;
},
_getModel: function () {
var transport = this,
i,
j,
fields = {},
schema,
value,
type,
element;
schema = transport.jsdo[transport.tableRef].getSchema();
for (i = 0; i < schema.length; i += 1) {
// Skip internal fields
if (schema[i].name.length > 0 && schema[i].name.charAt(0) !== "_") {
type = schema[i].type;
if (type === "integer") {
type = "number";
} else if (type === "string" &&
schema[i].format &&
schema[i].format.indexOf("date") !== -1) {
// Set type to "date" is type is DATE or DATETIME[-TZ]
type = "date";
}
if (type === "array") {
for (j = 0; j < schema[i].maxItems; j += 1) {
value = transport.jsdo._getDefaultValue(schema[i]);
element = transport.jsdo._getArrayField(schema[i].name, j);
if (!transport.jsdo[transport.tableRef]._fields[element.name.toLowerCase()]) {
// Skip element if a field with the same name exists
// Calculate type of array element
type = schema[i].items.type;
if (type === "integer") {
type = "number";
} else if (type === "string" && schema[i].ablType && (schema[i].ablType.indexOf("DATE") !== -1)) {
type = "date";
}
fields[element.name] = {};
fields[element.name].type = type;
if (value !== undefined) {
fields[element.name].defaultValue = value;
}
}
}
} else {
value = transport.jsdo._getDefaultValue(schema[i]);
fields[schema[i].name] = {};
fields[schema[i].name].type = type;
if (value !== undefined) {
fields[schema[i].name].defaultValue = value;
}
}
}
}
if (transport.jsdo._resource.idProperty) {
fields[transport.jsdo._resource.idProperty].editable = false;
}
return fields;
},
_getInitialValues: function () {
var transport = this,
i,
j,
data = {},
schema,
defaultValue;
schema = transport.jsdo[transport.tableRef].getSchema();
for (i = 0; i < schema.length; i += 1) {
// Skip internal fields
if (schema[i].name.length > 0 && schema[i].name.charAt(0) !== "_") {
defaultValue = transport.jsdo._getDefaultValue(schema[i]);
if (schema[i].type === "array") {
data[schema[i].name] = [];
for (j = 0; j < schema[i].maxItems; j += 1) {
data[schema[i].name][j] = defaultValue;
}
} else {
data[schema[i].name] = defaultValue;
}
}
}
return data;
},
_getData: function (options) {
var jsdo = this.jsdo,
data = {},
params,
array,
filter;
if (options && options.data) {
params = {
tableRef: this.tableRef,
filter: options.data.filter,
sort: options.data.sort,
skip: options.data.skip,
top: options.data.take
};
array = jsdo[this.tableRef].getData({filter: filter});
data.total = array.length;
array = jsdo[this.tableRef].getData(params);
data.data = array;
} else {
array = jsdo[this.tableRef].getData();
data.data = array;
data.total = array.length;
}
return data;
},
read: function (options) {
try {
var jsdo = this.jsdo,
filter,
data = {},
transport = this,
callback,
property,
optionsMapping = {
filter: "filter",
take: "top",
skip: "skip",
sort: "sort"
},
saveUseRelationships;
if (!this._initFromServer) {
if (jsdo[this.tableRef]._parent) {
this._initFromServer = (jsdo[jsdo[this.tableRef]._parent]._data && (jsdo[jsdo[this.tableRef]._parent]._data.length > 0))
|| (jsdo[this.tableRef]._data instanceof Array && (jsdo[this.tableRef]._data.length > 0));
} else {
this._initFromServer = (jsdo[this.tableRef]._data instanceof Array) && (jsdo[this.tableRef]._data.length > 0);
}
}
data.data = [];
if (this.readLocal && this._initFromServer) {
saveUseRelationships = jsdo.useRelationships;
jsdo.useRelationships = false;
data = this._getData(options);
jsdo.useRelationships = saveUseRelationships;
options.success(data);
return;
}
if (!this.readLocal) {
// readLocal is false or _initFromServer is false
if (options.data) {
// Only create filter object if options.data contains viable properties
for (property in options.data) {
if (options.data.hasOwnProperty(property)) {
if (options.data[property] !== undefined
&& optionsMapping[property]) {
if (filter === undefined) {
filter = {};
}
filter[optionsMapping[property]] = options.data[property];
}
}
}
if (filter) {
filter.tableRef = this.tableRef;
}
}
}
callback = function onAfterFillJSDO(object, success, request) {
var data = {}, saveUseRelationships, promise, total, exception, jsdo;
object = progress.util.Deferred.getParamObject(object, success, request);
jsdo = object.jsdo;
success = object.success;
request = object.request;
if (success) {
saveUseRelationships = jsdo.useRelationships;
jsdo.useRelationships = false;
if (transport.readLocal) {
// Use options.data to filter data
data = transport._getData(options);
} else {
data.data = jsdo[transport.tableRef].getData();
total = jsdo.getProperty("server.count");
if (total) {
data.total = total;
}
}
jsdo.useRelationships = saveUseRelationships;
transport._initFromServer = true;
if (options.data && options.data.take) {
if (!transport.readLocal &&
transport.countFnName !== undefined &&
typeof (jsdo[transport.countFnName]) === "function") {
if (options.data.skip === 0 && options.data.take > data.data.length) {
options.success(data);
return;
}
// Reuse filter string from the request.objParam object from fill() call.
promise = jsdo.invoke(
transport.countFnName,
{ filter: request.objParam.filter }
);
/*jslint unparam: true*/
promise.then(function (object, success, request) {
var exception, total, jsdo;
object = progress.util.Deferred.getParamObject(object, success, request);
jsdo = object.jsdo;
success = object.success;
request = object.request;
try {
if (typeof (request.response) === "object" &&
Object.keys(request.response).length === 1) {
total = request.response[Object.keys(request.response)];
if (typeof (total) !== "number") {
// Use generic exception if data type is not a number.
total = undefined;
}
}
} catch (e) {
// This exception is ignored a generic exception is used later.
}
if (total !== undefined) {
if (total) {
data.total = total;
}
options.success(data);
} else {
exception = new Error("Unexpected response from '"
+ transport.countFnName + "' operation.");
options.error(request.xhr, request.xhr.status, exception);
}
}, function (object, success, request) {
var exception, jsdo;
object = progress.util.Deferred.getParamObject(object, success, request);
jsdo = object.jsdo;
success = object.success;
request = object.request;
exception = new Error("Error invoking '"
+ transport.countFnName + "' operation.");
options.error(request.xhr, request.xhr.status, exception);
});
/*jslint unparam: false*/
} else {
options.success(data);
}
} else {
options.success(data);
}
} else {
exception = new Error("Error while reading records.");
options.error(request.xhr, request.xhr.status, exception);
}
};
jsdo.fill(filter).then(callback, callback);
} catch (e) {
options.error(null, null, e);
}
},
_processChanges: function (options, request) {
var jsdo = this.jsdo,
transport = this,
array,
i,
jsrecord,
id,
record;
if (options.batch) {
array = [];
if (options.data.created instanceof Array) {
// Process changes for idProperty case
if (jsdo._resource.idProperty) {
if (request.jsrecords) {
for (i = 0; i < request.jsrecords.length; i += 1) {
if (request.jsrecords[i].data["prods:rowState"] === "created") {
record = transport._convertDataTypes(request.jsrecords[i].data);
array.push(record);
}
}
}
if (options.data.created.length !== array.length) {
options.error(
null,
null,
new Error("Created record was not found in memory.")
);
return;
}
} else {
for (i = 0; i < options.data.created.length; i += 1) {
jsrecord = jsdo[transport.tableRef].findById(
options.data.created[i]._id
);
if (jsrecord) {
record = transport._convertDataTypes(jsrecord.data);
array.push(record);
} else if (jsdo.autoApplyChanges) {
options.error(
null,
null,
new Error("Created record was not found in memory.")
);
return;
}
}
}
}
options.success(array, "create");
array = [];
if (options.data.updated instanceof Array) {
for (i = 0; i < options.data.updated.length; i += 1) {
jsrecord = jsdo[transport.tableRef].findById(
options.data.updated[i]._id
);
if (jsrecord) {
record = transport._convertDataTypes(jsrecord.data);
array.push(record);
} else if (jsdo.autoApplyChanges) {
options.error(
null,
null,
new Error("Updated record not found in memory.")
);
return;
}
}
}
options.success(array, "update");
array = [];
if (options.data.destroyed instanceof Array) {
for (i = 0; i < options.data.destroyed.length; i += 1) {
jsrecord = jsdo[transport.tableRef].findById(
options.data.destroyed[i]._id
);
if (jsrecord && jsdo.autoApplyChanges) {
options.error(
null,
null,
new Error("Deleted record was found in memory.")
);
return;
}
}
}
options.success(array, "destroy");
} else {
if (jsdo._resource.idProperty) {
if (request
&& request.batch
&& request.batch.operations instanceof Array
&& request.batch.operations.length === 1) {
id = request.batch.operations[0].jsrecord.data._id;
} else if (request
&& request.jsrecords instanceof Array
&& request.jsrecords.length === 1) {
id = request.jsrecords[0].data._id;
}
} else {
id = options.data._id;
}
jsrecord = jsdo[transport.tableRef].findById(id);
if (jsrecord) {
record = transport._convertDataTypes(jsrecord.data);
options.success(record);
} else {
options.success({});
}
}
},
_saveChanges: function (options) {
var transport = this,
callback = function onAfterSaveChanges(object, success, request) {
var jsrecord,
xhr,
status,
exception,
jsdo;
// console.log("DEBUG: callback: ");
object = progress.util.Deferred.getParamObject(object, success, request);
jsdo = object.jsdo;
success = object.success;
request = object.request;
if (success) {
// _id is expected to be set for CUD operations
// Deleted records will not be found and data is expected to be undefined
transport._processChanges(options, request);
} else {
if (request.batch
&& request.batch.operations instanceof Array
&& request.batch.operations.length === 1) {
xhr = request.batch.operations[0].xhr;
status = request.batch.operations[0].xhr.status;
} else if (request.jsrecords) {
xhr = request.xhr;
status = request.xhr.status;
}
exception = new Error("Error while saving changes.");
options.error(xhr, status, exception);
}
};
if (this.autoSave) {
this.jsdo.saveChanges(this.jsdo._hasSubmitOperation).then(callback, callback);
} else {
this._processChanges(options);
}
},
create: function (options) {
var jsdo = this.jsdo,
jsrecord,
saveUseRelationships = jsdo.useRelationships;
options.batch = options.data.models instanceof Array;
try {
jsdo.useRelationships = false;
if (options.batch) {
options.error(
null,
null,
new Error("A newer version of Kendo UI is expected for batching support.")
);
} else {
jsrecord = jsdo[this.tableRef].add(options.data);
options.data._id = jsrecord.data._id;
this._saveChanges(options);
}
} catch (e) {
// Undo changes on thrown exception
if (jsdo.autoApplyChanges) {
jsdo[this.tableRef].rejectChanges();
}
options.error(null, null, e);
} finally {
jsdo.useRelationships = saveUseRelationships;
}
},
update: function (options) {
var jsdo = this.jsdo,
jsrecord,
saveUseRelationships = jsdo.useRelationships;
options.batch = options.data.models instanceof Array;
try {
jsdo.useRelationships = false;
if (options.batch) {
options.error(
null,
null,
new Error("A newer version of Kendo UI is expected for batching support.")
);
} else {
jsrecord = jsdo[this.tableRef].findById(options.data._id);
jsrecord.assign(options.data);
this._saveChanges(options);
}
} catch (e) {
// Undo changes on thrown exception
if (jsdo.autoApplyChanges) {
jsdo[this.tableRef].rejectChanges();
}
options.error(null, null, e);
} finally {
jsdo.useRelationships = saveUseRelationships;
}
},
destroy: function (options) {
var jsdo = this.jsdo,
jsrecord,
saveUseRelationships = jsdo.useRelationships;
options.batch = options.data.models instanceof Array;
try {
jsdo.useRelationships = false;
if (options.data.models instanceof Array) {
options.error(
null,
null,
new Error("A newer version of Kendo UI is expected for batching support.")
);
} else {
jsrecord = jsdo[this.tableRef].findById(options.data._id);
jsrecord.remove();
this._saveChanges(options);
}
} catch (e) {
// Undo changes on thrown exception
if (jsdo.autoApplyChanges) {
jsdo[this.tableRef].rejectChanges();
}
options.error(null, null, e);
} finally {
jsdo.useRelationships = saveUseRelationships;
}
},
submit: function (options) {
var jsdo = this.jsdo,
i,
jsrecord,
saveUseRelationships = jsdo.useRelationships;
options.batch = true;
try {
jsdo.useRelationships = false;
if (options.data.created instanceof Array) {
for (i = 0; i < options.data.created.length; i += 1) {
jsrecord = jsdo[this.tableRef].add(options.data.created[i]);
options.data.created[i]._id = jsrecord.data._id;
}
}
if (options.data.updated instanceof Array) {
for (i = 0; i < options.data.updated.length; i += 1) {
jsrecord = jsdo[this.tableRef].findById(options.data.updated[i]._id);
if (jsrecord) {
jsrecord.assign(options.data.updated[i]);
} else {
options.error(null, null, new Error("Record not found in memory."));
}
}
}
if (options.data.destroyed instanceof Array) {
for (i = 0; i < options.data.destroyed.length; i += 1) {
jsrecord = jsdo[this.tableRef].findById(options.data.destroyed[i]._id);
if (jsrecord) {
jsrecord.remove();
} else {
options.error(null, null, new Error("Record not found in memory."));
}
}
}
this._saveChanges(options);
} catch (e) {
// Undo changes on thrown exception
if (jsdo.autoApplyChanges) {
jsdo[this.tableRef].rejectChanges();
}
options.error(null, null, e);
} finally {
jsdo.useRelationships = saveUseRelationships;
}
}
});
// This defines the JSDO DataSource by specifying the schema, transport and reader for it.
// The "id" property is set to "_id" to enable CUD operations.
jQuery.extend(true, kendo.data, {
schemas: {
jsdo: {
type: "jsdo",
model: {
id: "_id"
}
}
},
transports: {
jsdo: JSDOTransport
},
readers: {
jsdo: JSDODataReader
}
});
}());
}
/*jslint nomen: false*/