Lazarus

Programming => Networking and Web Programming => Pas2JS => Topic started by: nomorelogic on May 22, 2022, 07:04:18 pm

Title: binding DOM elements in DoRun is not persistent?
Post by: nomorelogic on May 22, 2022, 07:04:18 pm
hi all
I'm playing with pas2js reproducing an example found in blaise pascal magazine.

Development environment is Lazarus 2.2.2 + Pas2Js 2.2.0.
Follow some code from attached project:
Code: Pascal  [Select][+][-]
  1. procedure TMyApplication.doRun;
  2.  
  3. begin
  4.   // Your code here
  5.   BindElements;
  6.   btnLogin.AddEventListener('click',@DoLoginCLick);
  7.  
  8.   Terminate;
  9. end;
  10.  
  11. procedure TMyApplication.BindElements;
  12. begin
  13.   Writeln('BindElements');
  14.   edtEmail:=TJSHTMLInputElement(GetHTMLElement('edtEmail'));
  15.   edtPassword:=TJSHTMLInputElement(GetHTMLElement('edtPassword'));
  16.   btnLogin:=TJSHTMLButtonElement(GetHTMLElement('btnLogin'));
  17. end;
  18.  
  19. procedure TMyApplication.doLoginClick(aEvent: TJSEvent);
  20. begin
  21.   Writeln('doLoginClick');
  22.   // BindElements;   // <- uncomment and all works!
  23.   writeln('edtEmail.Value = ', edtEmail.value);    // <- this give the error
  24.   DoServerLogin(edtEmail.Value,edtPassword.Value);
  25. end;
  26.  

At runtime, if I click on LoginButton I get this error:
Code: Pascal  [Select][+][-]
  1. project_login.lpr:45 Uncaught TypeError: Cannot read properties of undefined (reading 'value')
  2.     at Object.doLoginClick (project_login.lpr:45:40)
  3.     at HTMLButtonElement.cb (rtl.js:249:1)
  4.  

Anyway if I uncomment "// BindElements" from "procedure TMyApplication.doLoginClick(aEvent: TJSEvent)"  all works well...

Where I'm wrong?

thanks
nomorelogic


Title: Re: binding DOM elements in DoRun is not persistent?
Post by: nomorelogic on May 24, 2022, 09:47:38 am
I got the solution from Michael Van Canneyt
follows the solution

Quote
Normally the bind should be persistent. I use the described technique since several years meanwhile.
Please check if your program code contains an Application.Free after the Application.Run.
If it does, remove it.
Earlier versions of Lazarus generated this statement erroneously when using
the 'new web application' wizard.
TinyPortal © 2005-2018