views:

1877

answers:

2

Hi,

i'm reading the book manning - asp.net ajax in action. on page 54-58 client delegates and callbacks are described.

in short:

with the client delegate you can change the this variable in the event handler itself to reflect another object. (so it won't be the DOM object that triggered the event for DOM events)

with a callback you can pass a context object to your event handler that will be available in the event handler as the second argument/parameter.

with both you can "pass" an object to the event handler (be it this or the context object) that wouldn't be available the event handler (in the scope of it).

What is the different between these client delegates vs. callbacks?

To me it seems that:

only the object you want to be available in the event handler is accessible in different ways:

  • client delegate: this
  • callback: second argument

...and maybe that only the callback has the DomEvent(first parameter) , but not the client delegate?!??

Is this correct? Is that all?

Maybe you can explain the difference between client delegates and callbacks.

thanks, tobi

A: 

I'm not really sure about this but I think the callback is what happens when the page is submitted to the server.

The client create delegate is there to create a delegate for calling JavaScript methods.

The Function.CreateDelegate() method is really helpful.

Say you want to handle a button click event in JavaScript. The "this" object available in function that handles the button click is the button that was clicked.

Now say you have a JavaScript Object that captures the button click event and does something. Normally "this" refers to the JavaScript Object itself but since Object's method is used to handle the button click event "this" actually refers to the object that raised the event: the button.

To get around this the JavaScript apply method was created. So instead of capturing the button click event by specifying the JavaScript Object's method in the attachEvent/attachEventListener method (or the $addHandler() method in the Ajax.NET Framework), you'd actually use the apply() method with these attachEvent/attachEventListener. This changes the context so that "this" refers to the JavaScript Object instead of the Object that raised the event.

Now, the Function.CreateDelegate() method does more than just use the apply method in creating a client delegate.

Take a look at the code:

Function.createDelegate = function Function$createDelegate(instance, method) {
    /// <summary locid="M:J#Function.createDelegate" />
    /// <param name="instance" mayBeNull="true"></param>
    /// <param name="method" type="Function"></param>
    /// <returns type="Function"></returns>
    var e = Function._validateParams(arguments, [
    {name: "instance", mayBeNull: true},
    {name: "method", type: Function}
    ]);
    if (e) throw e;
    return function() {
      return method.apply(instance, arguments);
    }
}

There's one thing you should know about the apply() method. When you execute it, it executes the method that is the delegate. To get around this the createDelegate method does the above.

So, I'm not really sure how a callback fits into this, but using the createDelegate() method is a good idea when your JavaScript Objects handle events that are caused by some UI element.

I'm assuming that the callback is the action that takes place when the request is sent from the browser to the server. Most of the time the __doPostback method is used to preform the callback. This method takes 2 parameters...the object that caused the postback/callback to the server, and the arguments for the postback/callback. This would be a something completely different for the client delegate though.

Happy Coding

-Frinny

Frinavale
+1  A: 

I think this link will help you http://www.dennydotnet.com/post/ASPNET-AJAX-createDelegate-and-the-JavaScript-this.aspx

Venkat