Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Anonymous js function with callback to backend

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:

   '$("#{%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.

Hi You can solve this By Learning more about CallBacks

Please take a look at this documentation:

Look for RegisterCallBack

and check the demos:
1) Basic CallBack demo:
Look up specially for this two methods in Unit2.pas:


2) This is a best exmple of what you may want to achieve:
Check Unit1.pas:
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.

           $(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", $(
                        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:
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.

Forum Jump:

Users browsing this thread: 1 Guest(s)