Posts: 166
Threads: 47
Joined: Jan 2020
Reputation:
4
Location: Portugal
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??
Posts: 2,261
Threads: 196
Joined: Mar 2018
Reputation:
86
Location: Auckland, New Zealand
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.
Posts: 2,261
Threads: 196
Joined: Mar 2018
Reputation:
86
Location: Auckland, New Zealand
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");');
Posts: 2,261
Threads: 196
Joined: Mar 2018
Reputation:
86
Location: Auckland, New Zealand
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