Atozed Forums
Anonymous js function with callback to backend - Printable Version

+- Atozed Forums (https://www.atozed.com/forums)
+-- Forum: Atozed Software Products (https://www.atozed.com/forums/forum-1.html)
+--- Forum: IntraWeb (https://www.atozed.com/forums/forum-3.html)
+---- Forum: English (https://www.atozed.com/forums/forum-16.html)
+----- Forum: IntraWeb General Discussion (https://www.atozed.com/forums/forum-4.html)
+----- Thread: Anonymous js function with callback to backend (/thread-498.html)



Anonymous js function with callback to backend - mschumann - 07-13-2018

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!


RE: Anonymous js function with callback to backend - fduenas - 07-13-2018

Hi You can solve this By Learning more about CallBacks

Please take a look at this documentation:
http://docs.atozed.com/docs.dll/classes/TIWApplication.html

Look for RegisterCallBack

and check the demos:
1) Basic CallBack demo:
https://github.com/Atozed/IntraWeb/tree/master/XIV/Delphi/CustomAjaxCall
Look up specially for this two methods in Unit2.pas:

TIWForm2.DoMyAjaxFunc
TIWForm2.IWAppFormCreate

2) This is a best exmple of what you may want to achieve:
https://github.com/Atozed/IntraWeb/blob/master/XIV/Delphi/NewDialogs/Unit1.pas
Check Unit1.pas:


RE: Anonymous js function with callback to backend - mschumann - 08-19-2018

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')
                    })
               ...  



RE: Anonymous js function with callback to backend - mschumann - 08-24-2018

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...


RE: Anonymous js function with callback to backend - Jose Nilton Pace - 08-24-2018

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);' );



RE: Anonymous js function with callback to backend - mschumann - 08-25-2018

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.


RE: Anonymous js function with callback to backend - Jose Nilton Pace - 08-25-2018

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