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
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) }
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
Posted by: Brian C | July 18, 2008 at 10:45 AM
@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.
Posted by: Brian Holmes | July 18, 2008 at 10:48 AM
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?
Posted by: Mark F | August 21, 2008 at 06:03 PM
@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.
Posted by: Brian Holmes | August 21, 2008 at 07:49 PM