Hello,
I am trying to integrate an anonymous function into a js component that calls the backend and returns a value depending on the state of the backend. I am using IW 15 and Delphi 10.2.3 Enterprise and IWBS.
Within the Bootstrap FIle Input I attach an anonymous function:
Code:
'$("#{%htmlname%}").on("filebeforedelete", function() {' +
'return !window.confirm("Do you really want to delete the file?");});';
This works well. But in my application I want to go back to the backend, look, whether the file may be deleted at all and in case of yes, display the boostrap dialog instead of the native confirm box.
As everything runs asynchonous, I am a bit lost and have no Idea on how to accomplish this with calls to the backend.
Any help is very appreciated.
Thanks!
Thanks for your reply, please excuse my late reaction.
Callbacks I already use. What I would like to achieve is to return a value from the backend to the script. Unfortunately it is not visible how I can do that with ExecuteAjaxEvent.
It is important to return to the caller as I cannot initiate the rest of the function from within the Delphi part, especially if it is an attached anonymous function.
An other example would be to callback into the Backend on rightclick, get configuration for am menu from the backend then continue displaying that menu in JavaScript.
Code:
$(this).on("contextmenu", function (e) {
// return native menu if pressing control
if (e.ctrlKey) return;
//open menu
var wurst = executeAjaxEvent("&id="+curID, null,"#{%htmlname%}.OnMenuCallback",
false, null, false);
var $menu = $(settings.menuSelector)
.data("invokedOn", $(e.target))
.show()
.css({
position: "absolute",
left: getMenuPosition(e.clientX, 'width', 'scrollLeft'),
top: getMenuPosition(e.clientY, 'height', 'scrollTop')
})
...
After doing a lot of experiments I learned that it is not feasable to have a synchrnous callback into the backend.
I decided to return false in these cases to abort the operation and call the backend. The backend then initiates the operation if allowed and confirmed by the user. In case of the file input it deletes the file in the database and then refreshes the file input control to reread the contents. In case of the menu it creates the required code and calls Javascript to open the menu.
I learned that is is essential to learn that everything between frontend and backend has to be asynchronous and - with a little thinking - this is possible in most cases. I was lead to the wrong way through many years synchronous fat client development...
Hi, you can send commands from backend to frontend, something like this:
Code:
WebApplication.CallBackResponse.AddJavaScriptToExecute( 'FindElem("IWCOMBOBOX1").selectedIndex = 0;' );
WebApplication.CallBackResponse.AddJavaScriptToExecute( '$("#EditModal").modal("show");' ) ;
WebApplication.CallBackResponse.AddJavaScriptToExecuteAsCDATA( 'SubmitClick("IWBUTTON1", "", true);' );
Yes thanks, this I also use.
What I cannot do is call the backend from the frontend within a function and return a value as if it was a javascrpt function.
For example in a "onContextMenu" js event of a non IW Control.
If I understood what you need. You can create a
CustomContentHandlers inside the application and consume it inside the JS function, taking the result of it.
Look
this demo.
http://127.0.0.1:8888/GiveMeSomeXML