What's the difference between WebApplication.CallBackResponse.AddJavaScriptToExecuteAsCDATA and WebApplication.AddToInitProc ?
Can someone explain to me?
When should use one instead of the other and vice-versa??
AddToInitProc is a method of the IWForm (and also other objects like TIWPageContext), but it's not present in TIWApplication (WebApplication) interface.
AddToInitProc adds JavaScript code to the initialization section of the rendered page (function Initialize() that is called when page ends loading, similarly to jQuery's OnReady)
AddJavaScriptToExecute (and AddJavaScriptToExecuteAsCDATA) are methods from CallBackResponse which add JavaScript that is executed when a response to an Async request is generated.
(12-20-2021, 02:10 AM)Alexandre Machado Wrote: [ -> ]AddToInitProc is a method of the IWForm (and also other objects like TIWPageContext), but it's not present in TIWApplication (WebApplication) interface.
AddToInitProc adds JavaScript code to the initialization section of the rendered page (function Initialize() that is called when page ends loading, similarly to jQuery's OnReady)
AddJavaScriptToExecute (and AddJavaScriptToExecuteAsCDATA) are methods from CallBackResponse which add JavaScript that is executed when a response to an Async request is generated.
Still a little bit confused.
Let's supose that I need to refresh, using javascript, a IWBSSelect component, wich one should I choose,
AddToInitProc or AddJavaScriptToExecuteAsCDATA ?
Javascript code:
$('#MyIWBSSelect').selectpicker('refresh');
AddToInitProc() is meant to be used in sync requests, while AddJavaScriptToExecute() is meant to be used in async calls.
If your code needs to be shared between sync/async calls, you can use the TIWControlHelper class:
Just declare IWControlHelper in your uses clause and use the extension method:
procedure AddJavaScriptToExecute(const jsCode: string);
This method will be available in all TIWControl descendants (the TIWControlHelper class is a "real" helper class and follows the same rules as any other helper class in Delphi)
So you can use:
IWBSSelect.AddJavaScriptToExecute('$("#MyIWBSSelect").selectpicker("refresh");');
(05-13-2022, 10:11 PM)Alexandre Machado Wrote: [ -> ]AddToInitProc() is meant to be used in sync requests, while AddJavaScriptToExecute() is meant to be used in async calls.
If your code needs to be shared between sync/async calls, you can use the TIWControlHelper class:
Just declare IWControlHelper in your uses clause and use the extension method:
procedure AddJavaScriptToExecute(const jsCode: string);
This method will be available in all TIWControl descendants (the TIWControlHelper class is a "real" helper class and follows the same rules as any other helper class in Delphi)
So you can use:
IWBSSelect.AddJavaScriptToExecute('$("#MyIWBSSelect").selectpicker("refresh");');
Thanks for the explanation, Alexandre!!
But sometimes, only AddToInitProc do the job, while AddJavaScriptToExecute doesn't do nothing.
For instance, when I press a button (ASyncClick) and have many operation, needing to update/refresh several components, when using AddJavaScriptToExecute do update/refresh enabled state of a IWSelect component, it won't work. I have to call AddToInitProc to work as expected.
WebApplication.CallbackResponse.AddJavaScriptToExecute() only works from Async calls
however,
TIWControl.AddJavaScriptToExecute() (declaring IWControlHelper in uses class to have access to the helper methods), should work the same way in both Sync and Async.
TIWControlAddJavaScriptToExecute() will call AddToInitProc() internally if the call is a sync call