-
Notifications
You must be signed in to change notification settings - Fork 8
/
DX.WebLib.XData.pas
204 lines (179 loc) · 5.4 KB
/
DX.WebLib.XData.pas
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
unit DX.WebLib.XData;
interface
uses
System.SysUtils, System.Classes, System.Types,
JS, Web,
Data.DB,
XData.Web.JsonDataset,
XData.Web.Dataset,
XData.Web.Client,
DX.WebLib.SysUtils;
type
/// <summary>
/// This helper introduces a "RefreshData" method, that loads or refreshes the configures entity set from the server and provides a "ADoneProc", to implement sequential behaviour.
/// </summary>
TTXDataWebDataSetHelper = class helper for TXDataWebDataSet
public
/// <summary>
/// RefreshData is very similiar to "Load", but offers an additional callback, once the dataset finished loading its data form the server. <br />
/// </summary>
[async]
procedure RefreshData(ADoneProc: TProc); overload;
[async]
procedure RefreshData(const AServiceOperation: string; AParams: Array of JSValue; ADoneProc: TProc); overload;
end;
TXDataClientResponseHelper = class helper for TXDataClientResponse
function ResultValue: JSValue;
end;
function XDataClientResponse(AValue: JSValue): TXDataClientResponse;
implementation
{ TTXDataWebDataSetHelper }
{$IFDEF PAS2JS}
// Class cracker to access protected "OnSuccess" method
type
TXDataDataRequestX = class(TXDataDataRequest);
{$ENDIF}
procedure TTXDataWebDataSetHelper.RefreshData(ADoneProc: TProc);
// Some helper functions, taken from XData.Web.Dataset Javascript version
{$IFDEF PAS2JS}
function QueryToObject(const Query: string): TJSObject;
var
Params: TStringDynArray;
P: Integer;
I: Integer;
begin
Result := TJSObject.new;
Params := TJSString(Query).split('&');
for I := 0 to Length(Params) - 1 do
begin
P := Pos('=', Params[I]);
if P > 1 then
Result[Copy(Params[I], 1, P - 1)] := Copy(Params[I], P + 1, MaxInt);
end;
end;
function ObjectToQuery(Obj: TJSObject): string;
var
Key: string;
begin
Result := '';
for Key in TJSObject.keys(Obj) do
begin
if Result <> '' then
Result := Result + '&';
Result := Result + Key + '=' + JS.ToString(Obj[Key]);
end;
end;
function BuildQueryString: string;
var
Query: TJSObject;
begin
Query := QueryToObject(Self.QueryString);
if Self.QueryTop <> 0 then
Query['$top'] := IntToStr(Self.QueryTop);
if Self.QuerySkip <> 0 then
Query['$skip'] := IntToStr(Self.QuerySkip);
if Self.ServerRecordCountMode = smInlineCount then
Query['$inlinecount'] := 'allpages';
Result := ObjectToQuery(Query);
end;
{$ELSE}
function BuildQueryString: string;
begin
// Implemented with JS only
Result := '';
end;
{$ENDIF}
var
LClient: TXDataWebClient;
begin
if Self.EntitySetName = '' then
raise Exception.Create(Self.Name + ': Entity set not assigned!');
console.debug('Refreshing ' + Self.EntitySetName);
if not Assigned(Self.Connection) then
raise Exception.Create('Connection not assigned!');
await(Connection.OpenAsync);
LClient := TXDataWebClient.Create(nil);
try
LClient.Connection := Self.Connection;
LClient.List(EntitySetName, BuildQueryString,
procedure(AResponse: TXDataClientResponse)
{$IFDEF PAS2JS}
// Implemented with JS only
var
LDataProxy: TXDataDataProxy;
{$ENDIF}
begin
if (AResponse.StatusCode = 200) then
begin
Close;
Self.SetJsonData(AResponse.Result);
// FServerRecordCount is private and can only be set on JS with some trickery
{$IFDEF PAS2JS}
LDataProxy := TXDataDataProxy.Create(Self);
try
TXDataDataRequestX(LDataProxy.GetDataRequest([], nil, nil)).OnSuccess(AResponse);
finally
FreeAndNil(LDataProxy);
end;
{$ENDIF}
Self.Open;
console.debug(Self.EntitySetName + ' refreshed.');
console.debug('RecordCount: ' + Self.RecordCount.ToString + ' ServerRecordCount: ' +
Self.ServerRecordCount.ToString);
if Assigned(ADoneProc) then
begin
ADoneProc;
end;
end
else
begin
// Todo: OnErrorProc?
console.error('RefreshData failed: ' + Self.Name + ' - ' + AResponse.Response.StatusReason);
end;
end);
finally
FreeAndNil(LClient);
end;
end;
procedure TTXDataWebDataSetHelper.RefreshData(const AServiceOperation: string; AParams: Array of JSValue;
ADoneProc: TProc);
var
LResponse: TXDataClientResponse;
LClient: TXDataWebClient;
begin
if not Assigned(Connection) then
begin
console.error(Self.Name + ': Connection not assigned');
exit;
end;
Self.Close;
LClient := TXDataWebClient.Create(nil);
try
LClient.Connection := Self.Connection;
LResponse := XDataClientResponse(await(LClient.RawInvokeAsync(AServiceOperation, AParams)));
if LResponse.Response.StatusCode >= 300 then
begin
console.error(AServiceOperation + ': ' + LResponse.Response.StatusReason);
exit;
end;
// ResultValue ist ein Helper der das 'Value'-Objekt liefert.
Self.SetJsonData(LResponse.ResultValue);
Self.Open;
console.debug(AServiceOperation + ' RecCount: ' + Self.RecordCount.ToString);
finally
FreeAndNil(LClient);
end;
if Assigned(ADoneProc) then
begin
ADoneProc;
end;
end;
function XDataClientResponse(AValue: JSValue): TXDataClientResponse;
begin
result := TXDataClientResponse(AValue);
end;
function TXDataClientResponseHelper.ResultValue: JSValue;
begin
result := self.ResultAsObject['value'];
end;
end.