BitterSweetJaVa

March 3, 2009

Assign events programmatically in Flex

Filed under: Flex — Tags: , — .|2ic|K @ 4:55 PM

How can you write the line below programmatically?

<local:Item mouseMove="mouseMoveHandler(event)" prop1="value1" prop2="value2" />

Here’s the solution:

var item1:Item = new Item();
item1.prop1 = "value1";
item1.prop2 = "value2";
item1.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

Assuming that you have the following function:


public function mouseMoveHandler(event:MouseEvent) {
(. . .)
}

If you need to access to an attribute of the Item object inside the mouseMoveHandler function, then you can use the following:


var item:Item = event.target;

event.target will return the corresponding Item object.

February 25, 2009

Flex’s bindSetter function.

Filed under: Flex — Tags: , — .|2ic|K @ 5:55 PM

This is a very useful function in the Flex’s BindingUtils class. 

It allows to execute a function everytime the associated (and bindable) property change it’s value.

Here’s the syntax:

BindingUtils.bindSetter(bindFunction, hostObject, “propertyName”);

 
And the example…


private var:hostObject:MyObject;

private function init():void {
    BindingUtils.bindSetter(calculate, hostObject, "squareRoot");
}

private function calculate(number:Number):void {
    // ... calculate the square root here...
}

So, in the example above:

Every time the property with name squareRoot, inside the object hostObject has a new value assigned, the function calculate will be called.

Remarks:

  • squareRoot is a variable of type Number
  • The value of squareRoot will be passed as a parameter to the calculate  function.

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.