Delegate

An object encapsulating a reference to a method on another object.

Remarks

This class provides an easy way for creating an object that can be used to call a method on another object.

Methods

equalsTests if two delegates refer to the same method.
invokeInvokes the delegate.
invokeAfterInvokes this delegate after a delay.
invokeOnIdleInvokes this delegate just before returning from the current event handler.

Delegate equals method

Tests if two delegates refer to the same method.

JavaScript

var boolVal = delegate.equals(otherObj)

Returns

true if otherObj is another Delegate instance pointing to the same method this delegate points to. false otherwise.

Delegate invoke method

Invokes the delegate.

JavaScript

var obj = delegate.invoke()

Remarks

This method invokes the encapsulated method function on the object passed in to the delegate constructor.

Returns

The value returned by calling the function, or undefined if the function returns no value.

Example

To create a delegate, use:

var d = new Delegate(myObject, MyClass.prototype.methodName);

The object in d is now an alais for myObject's methodName method. i.e. writing:

d.invoke();

is then equivalent to writing:

myObject.methodName();

Delegate invokeAfter method

Invokes this delegate after a delay.

JavaScript

var obj = delegate.invokeAfter(delay)

Remarks

Call this to invoke this delegate object asynchronously after a wait of delay milliseconds.

Returns

A JavaScript object which has a cancel() method that can be used to cancel the invokeAfter.

Example

This invokes the myPopup function after a second:

function myPopupFunc() { alert('hello'); }
var delegate = new Delegate(null, myPopupFunc);
delegate.invokeAfter(1000);
You can save the result returned by invokeAfter and use it to cancel the delayed action:
function myPopupFunc() { alert('hello'); }
var delegate = new Delegate(null, myPopupFunc);
var t = delegate.invokeAfter(1000);
t.cancel(); // cancels the invokeAfter

Delegate invokeOnIdle method

Invokes this delegate just before returning from the current event handler.

JavaScript

delegate.invokeOnIdle()

Remarks

Used to invoke the delegate on the next idle.