Async Event Handling

Last Updated: 5/7/2009



Sections above here:
Home  »  Development  »  Ajax

Sections below here:

    Topics in this section:
    IW Ajax Limitations
    Error Handling and Debugging
    Update Protocol
    Async Event Handling

    Search Documentation:

    Advanced Async Event Handling

    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;



    (C) 2002-2009 - Atozed Software Ltd.