10.23.09

Flex Label Linkability

Posted in Flex, Programming at 10:06 am by Justin

Turns out in order for HTML links in the htmltext property of Label/Text to work (both by using a direct link to a URL or by using the ‘link’ event), ’selectable’ must be set to to true. Dumb. Especially considering when it’s set to false, the links still give you the hand cursor, making it appear the link should work.

06.04.09

Capturing DataGrid Sort

Posted in Flex, Programming at 9:42 am by Justin

In a current project of mine, I needed to capture when a user clicks on a DataGrid header to sort it (to track the column and sort order). The only good event to listen to is “headerRelease”. However, if you just add a declarative event listener to the DataGrid, you’ll find in your handler the sort hasn’t been applied yet. This is because your handler will complete before the sort ever takes place, due to DataGrid adding its own handler for the event with a priority of -50.

In order for DataGrid’s handler to complete so that the sort will be applied for your handler, you’ll need to add your handler with a lower priority:

dataGrid.addEventListener( DataGridEvent.HEADER_RELEASE, dataGridHeaderReleaseHandler, false, -100 );

I chose to use -100 in this instance. If you check out the class EventPriority, you’ll find where DataGrid’s -50 is defined (this is the value used for all internal handlers of Flex core components), along with some other priorities used for core Flex components such as Effects, Binding and CursorManager.

Depending on your scenario, you may want to always add your handlers at a lower priority than the component’s internal handlers, unless there’s a chance you need to call “preventDefault()” in order to stop the component from performing default behavior.

05.21.09

ArrayCollection: Sort and Embellish

Posted in Flex, Programming at 11:05 am by Justin

Suppose for some reason you have an ArrayCollection that will contain a set of Strings that you want to have sorted. Then, you’d like to add an entry to the beginning and another at the end, but you don’t necessarily want the sort to apply.

var collection:ArrayCollection = new ArrayCollection([”a”,”z”,”m”]);
collection.sort = new Sort();
collection.refresh();
collection.source = collection.toArray();
collection.sort = null;
collection.refresh();
collection.addItemAt( “first”, 0 );
collection.addItem( “last” )

The key above is the line:

collection.source = collection.toArray();

This “locks” the sort by creating an Array in the order you want, rather than simply returning the elements sorted as you use the collection. If you don’t do this, when you remove the sort and refresh(), your collection will revert to its original order. Also be sure to call refresh() after you remove the sort or you’ll run into strange errors trying to add more items.

10.09.07

Forwarding a dynamic argument “…(rest)” array in Flex

Posted in Flex, Programming at 1:57 pm by Justin

If you’ve done much in flex, you may have used the “…” notation in a method signature to indicate that the function takes a dynamic number of arguments, such as:

private function doIt( ...args ):void

… then called the method:

doIt( 1 )
doIt( 1, 2 )
doIt( "some", "other", "stuff" )

The question is this: from within that method, how do you send those same arguments on to another method that accepts a dynamic argument array? You can’t just pass the “args” array, as the method called will only see 1 argument; the array you passed it. Instead, you need to “explode” the array into its’ original parts. Here’s the most simple way I’ve found to forward that array on to the next method:

public function doIt( ...args ):void
{
   var someObject:SomeType = new SomeType()
   someObject.someMethod.apply( someObject, args );
}

The “apply” method of Function does exactly what we need. Now “someMethod” will have it’s own argument array, and be able to access them the same way you were able to from within “doIt”.

This all came about when we began writing a wrapper of sorts for WebServices. We created our own RPC class, with a “call” method that accepts the parameters, and we needed to forward them on to Operation’s “send” method. Below is a much simplified example:

import mx.rpc.soap.Operation;
public class RPC
{
  public static function call( operation, ...params ):void
  {
    //...code to initialize WebService "svc"
    var op:Operation = Operation( svc.getOperation(operation) );
    //...more code (unimportant)
    var token:AsyncToken = op.send.apply( op, params  );
    //...code to hook up handlers
  }
}