[Pas2js] TDataset load local JSON in a async way. How?
warleyalex
warleyalex at yahoo.com.br
Fri Aug 28 23:17:49 CEST 2020
Following this piece of code:
fConnection.Active := true; // fDataSet should be Active BUT is still
FALSE
console.log( fDataset.Active ); // we've got a FALSE status because we
are loading JSON in async way
The ugly workaround, I have to use the setTimeout with 50ms
window.setTimeout(procedure()
begin
console.log( fDataset.Active ); // we've got TRUE status
end, 50);
[CODE]
var
i, aux: Integer;
begin
fConnection:= TConnection.Create(nil);
fConnection.Name := 'Connection1';
fConnection.Active := false;
fDataSet:= TDataSet.Create(nil);
fDataSet.Name := 'DataSet1';
fDataSet.Connection := fConnection;
fDataSource:= TWDataSource.Create(nil);
fDataSource.Name := 'DataSource1';
fDataSource.DataSet := fDataSet;
fConnection.URI := 'data.json';
fConnection.DataNode := 'data';
fDataSet.FieldDefs.Clear();
fDataSet.FieldDefs.Add('Species_No', TFieldType.ftString,0);
fDataSet.FieldDefs.Add('Category', TFieldType.ftString,50);
fDataSet.FieldDefs.Add('Common_Name', TFieldType.ftString,50);
fDataSet.FieldDefs.Add('Species_Name', TFieldType.ftString,50);
fDataSet.FieldDefs.Add('Length__cm_', TFieldType.ftInteger,0);
fDataSet.FieldDefs.Add('Length_In', TFieldType.ftString,30);
fDataSet.FieldDefs.Add('Notes', TFieldType.ftString,255);
fConnection.Active := true;
console.log( fDataset.Active); // we've got a FALSE
window.setTimeout(procedure()
begin
(* initialize / fill the first TFishRecord *)
console.log( fDataset.Active); // we've got TRUE status
fishRecord := JSON2TFishRecord(
TJSObject(fDataSet.Rows.Elements[0])
);
(* update de controls *)
refreshFacts();
end, 50);
end;
//-------------------------------------------------
(...)
{ TConnection }
function TConnection.fetchAsync(url: String): {TJSResponse} TJSPromise;
{$ifndef InLazIDE}async;{$endif}
begin
FResponse := await(window.fetch( url ));
Result := await(FResponse.json());
end;
procedure TConnection.SetActive(AValue: boolean);
var
J: JSValue;
JA: JSValue;
begin
if ((FURI <> '') and AValue) then
begin
DoBeforeConnect();
FActive := AValue;
fetchAsync(FURI)
._then(function(res: JSValue): JSValue
begin
if (FResponse.status = 200) then
begin
JA := TJSObject(res).Properties[FDataNode];
DoAfterConnect();
if (assigned(FDS)) then
begin
FDS.Rows := TJSArray(JA);
FDS.Open();
end else
DoError(FResponse.status);
end;
end);
[/CODE]
--
Sent from: http://pas2js.38893.n8.nabble.com/
More information about the Pas2js
mailing list