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

View Comments “Advanced IntraWeb Async Event Handling”
  1. Oliver Schulte says:

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

  2. Olaf Monien says:

    Thanks Oliver,

    the file should now be downloadable!

    Regards,
    Olaf

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

  4. Olaf Monien says:

    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.

  5.  
Leave a Reply


blog comments powered by Disqus
CodeGear Technology Partner