0

I am working on a code snipet where I have handlers like this:

_ratingHandler = Function.createDelegate(this, this.onRatingChange);
_endClientCallBackHandler = Function.createDelegate(this, this.onEndClientCallBack);

and I am using them like this:

function attachEvent()
 {
    _eventOk = false;
    _eventCallBackOk = false;
    rating1Extender.add_Rated(_ratingHandler);
    rating1Extender.add_EndClientCallback(_endClientCallBackHandler);
}

Now I want to conver this code to Jquery. what is the alternative of createDelegate in jquery and how I will modify my attevent function ?

Regards, Asif Hameed

Shoe
  • 74,840
  • 36
  • 166
  • 272
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316

1 Answers1

0

The equivalent of the Function.createDelegate function in jQuery is the proxy function:

Similar question on Stack

jQuery proxy API

So it might look something like this:

_ratingHandler = $.proxy(this.onRatingChange, this);
_endClientCallBackHandler = $.proxy(this.onEndClientCallBack, this);

Or this should work too:

_ratingHandler = $.proxy(this, "onRatingChange");
_endClientCallBackHandler = $.proxy(this, "onEndClientCallBack");

I don't think you will need to change you attachEvent method but without seeing more of the code it is tough to say.

Community
  • 1
  • 1
Adam Ayres
  • 8,732
  • 1
  • 32
  • 25
  • @Adam Ayres: I tried both and got undefined when I put alert(ratingHandler ). This is all the related code. – DotnetSparrow Mar 08 '11 at 11:19
  • Could it be that your alert is missing the _: `alert(_ratingHandler)`? What is the `this` object a reference to, can you show that code? Where are you applying the event handler? – Adam Ayres Mar 08 '11 at 11:24
  • this is window. and I am using attachEvent inside a JQuery QUnit Test Like this: module('Attach to onChangeRating'); asyncTest('Attach to onChangeRating', 1, function () { rating1Extender = $find('RatingBehavior1'); attachEvent(); ok(true, 'testing'); start(); }); – DotnetSparrow Mar 08 '11 at 11:30
  • Does the code extend the window object with the `onRatingChange` function or is the `onRatingChange` function on some other object? `onRatingChange' is not a native function so somewhere in your code it must be added. I am familiar with QUnit but you are still not providing enough of the code to troubleshoot the issue. I suggest you put the code in a [jsfiddle](http://jsfiddle.net/). – Adam Ayres Mar 08 '11 at 11:37
  • @Adam: onratingchange is related to ratingControl in ajaxtoolkit here is how it is defined – DotnetSparrow Mar 08 '11 at 11:51
  • Sorry, I am not familiar with aspext and this control. – Adam Ayres Mar 08 '11 at 20:09