Delegates

As you have seen, you can easily wire an event with an event handler by using the handler and method tags. This is not always the case, however. Some objects are created by using the new keyword, not by using its tag description. In such a case, the conventional way of wiring an event to an event handler cannot be used because there is no tag involved. OpenLaszlo however introduces the concept of delegates, which allows event wiring to occur dynamically.

A delegate, represented by the LzDelegate class in LZX, is an object used to bind an event with an event handler at runtime. The signature of the LzDelegate class’s constructor is as follows:

LzDelegate(context, functionName, eventSender, eventName)

Here, context is the reference to the object to be called (the object that contains the event handler), functionName the name of the event handler, eventSender the object that raises the event, and eventName the event to register this delegate to. Both eventSender and eventName are optional, but the latter must be present if the former is present.

If an instance of LzDelegate is created without the eventName and eventSender arguments, the delegate can be registered to an event by using the delegate’s register method.

Table 5.3 shows the attributes defined in LzDelegate.

Table 5.3. The attributes defined in the LzDelegate class
NameUsageTypeDefaultAccessibility
cJS onlyObject read-only
 Description. The context in which to invoke the method.
fJS onlystring read-only
 Description. The method to call.

The LzDelegate class has the following methods:

disable()

Disables the delegate.

enable()

Enables the delegate.

execute(data)

Executes the name method in the given context with the specified data.

register(eventSender, eventName)

Registers the delegate for the specified event in the given context.

unregisterAll()

Deregisters the delegate for all of the events that have been registered. unregisterFrom(event)

Deregisters the delegate for the specified event.

For example,

var del = new LzDelegate( this, "changeText", b, "onclick");

is the same as

var del = new LzDelegate( this, "changeText");
del.register(b, "onclick");

For instance, the code in Listing 5.8 is an LZX application that uses a delegate to bind an event with an event handler.

Listing 5.8. Using a delegate
<canvas height="60">
    <button id="b">Click Here</button>
    <method name="changeText">
        b.setAttribute("text", "Hello");
    </method>

    <handler name="oninit">
        var del = new LzDelegate( this, "changeText", b, "onclick");
    </handler>
</canvas>

In this case, the oninit event’s handler is triggered when the canvas is initialized, creating a delegate object that binds the button’s onclick event with the changeText method.

Admittedly, the code in Listing 5.8 is contrived, because it is easier to bind the button with the handler at compile time. However, the LZX application in the following section illustrates a real-life use of delegates.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset