« Global classes and static global functions sans import | Main | Bedlam Break »

April 16, 2008

My Struct() Custom Data Type that Binds

I’ve wanted to have a struct (hashtable) class that I can use for some time that also bind to controls like List, DataGrid, etc… Dictionary works fine, it just didn’t update the bindings. I just hadn’t figured out how to make sure how to make sure it’s dispatching events.  But I finally got around to creating a class that behaves more like a cold fusion struct that I can bind to. You can’t bind to it using brackets, but you can read values by key and set values by key. To bind by key there’s a method you have to go through.  You can get the class here

Struct.as (1 KB)

Here’s a brief run down of use cases:

Create a new Struct

[Bindable]

public var struct : Struct = new Struct();

Set a value 

struct[key] = value or struct.addByKey(key,value);

Read a value

struct[key] or struct.getByKey(key);

Bind to a value by key

{ struct.getByKey(key) }

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/2357234/28186650

Listed below are links to weblogs that reference My Struct() Custom Data Type that Binds:

Comments

Brian,

What are some situations where using a bindable struct would be more beneficial then using an ArrayCollection as the datasource?

I used to use structs all the time in CF, but now in Flex I can't remember why :-)

-BC

@Brian C, Use the Struct class when you want to bind to the data collection but still need to look things up by an item name. Otherwise you have to loop over all the items to find the one you want. My Struct class actually extends ArrayCollection so you can treat it the same way.

Brian,

Can you see a way to extend this for use in a heirarchical dataProvider to that any node, no matter how deep, can be located by a key referencing the dataProvider?

@Mark F, If you could assume all keys inside the hierarchical data were unique that would be possible. You just need to modify the addByKey(), getByKey() methods. As long as they know how to traverse your data you could look anything up. There might be easier ways of doing that however, if you had structs of structs, your references might be easier to manage. myStruct[key][subkey] for example.

Post a comment

If you have a TypeKey or TypePad account, please Sign In