Jump to content

Function Return Value issue with AjaxEvent


SEGNA

Recommended Posts

Hi,

In my test project, I have a function in AfterScript in UniHtmlFrame.
I can call a Function and define a variable with UniFormAjaxEvent. There is no problem until this point.

But when I click on a button I want to get the Result value returned from the function to Result.Text box. "Result.Text := js(Param.Text);".
When Unisession.AddJS runs, I assign ReturnValue in AjaxEvent. But when the function is finished, AjaxEvent assigns it.
Thus, it returns the value as the previous value.
Sample test project is attached.

Thank you.

 

unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIRegClasses, uniGUIForm, uniButton, uniGUIBaseClasses,
  uniEdit, uniPanel, uniHTMLFrame, uniLabel;

type
  TMainForm = class(TUniForm)
    result: TUniEdit;
    UniButton1: TUniButton;
    UniHTMLFrame1: TUniHTMLFrame;
    param: TUniEdit;
    UniLabel1: TUniLabel;
    UniLabel2: TUniLabel;
    procedure UniButton1Click(Sender: TObject);
    procedure UniFormAjaxEvent(Sender: TComponent; EventName: string;
      Params: TUniStrings);
  private
    ReturnValue : string;
    function js(test: string): string;

  public
    { Public declarations }
  end;

function MainForm: TMainForm;

implementation

{$R *.dfm}

uses
  uniGUIVars, MainModule, uniGUIApplication;

function MainForm: TMainForm;
begin
  Result := TMainForm(UniMainModule.GetFormInstance(TMainForm));
end;

function TMainForm.js(test: string): string;
begin
    UniSession.AddJS('myFunction('+ test +')');
    Result := ReturnValue;
end;

procedure TMainForm.UniButton1Click(Sender: TObject);
begin

    result.Text := js(param.Text);

end;

procedure TMainForm.UniFormAjaxEvent(Sender: TComponent; EventName: string;
  Params: TUniStrings);
begin
    if EventName = '_test' then
    begin
      ReturnValue := Params.Values['Value'];
    end;
end;

initialization
  RegisterAppFormClass(TMainForm);

end.

 

Test.rar

Link to comment
Share on other sites

58 minutes ago, SEGNA said:
function TMainForm.js(test: string): string;
begin
    UniSession.AddJS('myFunction('+ test +')');
    Result := ReturnValue;
end;

procedure TMainForm.UniButton1Click(Sender: TObject);
begin

    result.Text := js(param.Text);

end;

procedure TMainForm.UniFormAjaxEvent(Sender: TComponent; EventName: string;
  Params: TUniStrings);
begin
    if EventName = '_test' then
    begin
      ReturnValue := Params.Values['Value'];
    end;
end;

Hello,

This approach won't work...

I will try to provide an example.

Link to comment
Share on other sites

procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  UniSession.AddJS('function doTest(){ajaxRequest(MainForm.form, ["test"], { data: 58 });};doTest();');
end;

procedure TMainForm.UniFormAjaxEvent(Sender: TComponent; EventName: string;
  Params: TUniStrings);
begin
  if EventName = 'test' then
    ShowMessage(Params.Values['data']);
end;

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...