-
Notifications
You must be signed in to change notification settings - Fork 0
/
dx.data.linq.js
93 lines (75 loc) · 2.64 KB
/
dx.data.linq.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
// https://github.com/DevExpress/DevExtreme-Data-LINQ
// Copyright (c) Developer Express Inc.
(function($, DX) {
function createDataSource(options) {
var loadUrl = options.loadUrl,
updateUrl = options.updateUrl,
insertUrl = options.insertUrl,
deleteUrl = options.deleteUrl,
config = { };
config.key = options.key;
config.load = function(loadOptions) {
var d = new $.Deferred();
$.getJSON(loadUrl, loadOptionsToActionParams(loadOptions))
.done(function(res) {
if($.isArray(res))
d.resolve(res);
else
d.resolve(res.data, { totalCount: res.totalCount })
});
return d.promise();
};
config.totalCount = function(loadOptions) {
return $.getJSON(loadUrl, loadOptionsToActionParams(loadOptions, true))
};
if(updateUrl) {
config.update = function(key, values) {
return $.ajax(updateUrl, {
method: options.updateMethod || "put",
data: {
key: serializeKey(key),
values: JSON.stringify(values)
}
});
};
}
if(insertUrl) {
config.insert = function(values) {
return $.ajax(insertUrl, {
method: options.insertMethod || "post",
data: { values: JSON.stringify(values) }
});
};
}
if(deleteUrl) {
config.remove = function(key) {
return $.ajax(deleteUrl, {
method: options.deleteMethod || "delete",
data: { key: serializeKey(key) }
});
};
}
return new DevExpress.data.CustomStore(config);
}
function loadOptionsToActionParams(options, isCountQuery) {
var result = {
requireTotalCount: options.requireTotalCount,
isCountQuery: isCountQuery,
skip: options.skip,
take: options.take,
};
if($.isArray(options.sort))
result.sort = JSON.stringify(options.sort);
if($.isArray(options.filter))
result.filter = JSON.stringify(options.filter);
return result;
}
function serializeKey(key) {
if(typeof key === "object")
return JSON.stringify(key);
return key;
}
$.extend(DX.data, {
linq: { createDataSource: createDataSource }
});
})(jQuery, DevExpress);