Skip to content Skip to sidebar Skip to footer

Asp.net Ajax Function.createdelegate Vs Function.createcallback?

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 variab

Solution 1:

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 = functionFunction$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;
    returnfunction() {
      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

Post a Comment for "Asp.net Ajax Function.createdelegate Vs Function.createcallback?"