Implementing Asynchronous events with IntraWeb is plain and easy. Just hook some Pascal (or C++) code to any of the OnAsync Event handlers and you are done. The Advanced Ajax Events demo shows how to call Async events from your custom JavaScript routines and how to implement an asynchronous Key event handler.

This is all you need to Ajax-enable your IntraWeb application:

procedure TIWForm2.IWButton1AsyncClick(Sender: TObject;  EventParams: TStringList);
begin
  LabelResponse.Caption := 'Hello Ajax World!'
end;

Very simple, but what if you want to call async events from your own custom JavaScript? How to connect key events to some async IntraWeb handler?

This is the Delphi side of an async event handler, that listens to CTRL-A in the Web browser:

procedure TIWForm2.IWAppFormCreate(Sender: TObject);
begin
  WebApplication.RegisterCallBack('OnCtrlA', OnCtrlA);
end;

procedure TIWForm2.OnCtrlA(EventParams: TStringList);
begin
  WebApplication.ShowMessage('You pressed [CTRL - A] - Edit value: '+ IWEdit1.Text);
end;

The code in the Form’s JavaScript property implements the JavaScript side and shows how to call a “CallBack” (aka Delphi Async event handler) manually:

function KeyDownHandler(event){
 //Hide for CTRL-A key from browser
 if (event.keyCode==65 && event.ctrlKey)
 {
 event.returnValue = false;
 return false;  }
else {
 return true;
 }};

function KeyUpHandler(event){
 //check for CTRL-A key
 if (event.keyCode==65 && event.ctrlKey) {
   processAjaxEvent(event, null,"OnCtrlA",false, null, true);
   //hide from further "external" handling
   event.returnValue = false;
   return false;
  } else {
    return true;
  } };
//connect event handlers
window.onkeyup = KeyUpHandler;
window.onkeydown = KeyDownHandler;

Below is a more complex demo project. Please note that there are differences between browsers (IE, FF, Opera …). The demo takes care of that.

AdvancedAjaxEvents.zip

  • Oliver Schulte

    Hallo, die Datei AdvancedAjaxEvents.zip ist leider nicht downloadbar. Könnten Sie das beheben ? Wäre supertoll :)
    Besten Dank
    Oliver

  • http://www.monien.net/blog Olaf Monien

    Thanks Oliver,

    the file should now be downloadable!

    Regards,
    Olaf

  • http://www.atcc.net/ General Tackett

    what other things can you do with registercallback? How do you know what items you can use as arguments?

  • http://www.monien.net/blog Olaf Monien

    RegisterCallBack tackes just 2 params:
    1: CallBackName:string – will be used as name to identify the callback function. This name needs to be unique.
    2: A method reference of type method of object(EventParams: TStringList).

    On the client side you can then call processAjaxEvent(event, null,”CallBackName”,false, null, true); to call the newly registered server-side method.

  • Xzs2008

    so simple! the xml will be response to the browser.

    but some questions, how to response the json to browser directly? I want to use jquery method “getJSON”.

  • http://www.monien.net/blog Olaf Monien

    You could create a Custom IW component, which renders itself (partially) as JSon and have hooks, that would parse that JSon on the browser side and have it “paint” they way you want.

    Regards / Grüße,
    Olaf Monien
    ——————————————
    Developer Experts, LLC
    http://www.developer-experts.net
    http://www.monien.net/blog

    Office USA
    10600 Chevrolet Way #211
    Estero, FL 33928
    Fon: +1 (239) 494 5049

    Office Germany
    Daniel Magin
    Gerhart-Hauptmann-Ring 134
    60439 Frankfurt am Main
    Fon: +49 (69) 175 548 150

    Am 19.11.2010 um 22:19 schrieb Disqus:

  • http://www.wheelmax.com Wheels For Sale

    I am trying to be a professional programmer and for this I need to know all of this things. Thanks and I also want more of them here.

blog comments powered by Disqus
CodeGear Technology Partner