coder and technology lover
ObjectCollector: Accessing Flex’s objects by id (even dynamic generated) from anywhere
Ok, let me explain my last experiment…
Problem
Flex’s components have a nice id property which can be used to easily referencing object created.
All you know, that if I get the following mxml Application file:
1 2 3 4 5 6 | <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"> <mx:TextInput id="input1" width="300" /> </mx:Application> |
I can then create a Script block into which I’ll point to TextInput in this way:
1 | input1.text = "text dynamically added :)"; |
Perfect, but how can I reference dynamically created objects (components outside Application)?
The simplest example I can do is the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"> <mx:Script> <![CDATA[ private function test():void { var input:TextInput = new TextInput(); input.id = "input2"; this.addChild(input); // Here I get an error which says input2 is undefined! input2.text = "foo"; } ]]> </mx:Script> </mx:Application> |
Solution
Define a place where to store references to desired objects, which will be accessible through id from anywhere.
So, I created a singleton class called ObjectCollector which provides a simple, elegant and secure solution to the problem. You can use it in this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | // From anywhere you first create and register objects: var collector:ObjectCollector = ObjectCollector.getInstance(); var myObj1:Button = new Button(); myObj1.width = 300; myObj1.label = "my first button"; // id is mandatory, otherwise you'll get an error myObj1.id = "myFirstButton"; var myObj2:TextInput = new TextInput(); myObj2.text = "hello world"; // id is mandatory, otherwise you'll get an error myObj2.id = "myTextField"; collector.registerObject(myObj1); collector.registerObject(myObj2); /* ...then in another (or the same) place you can get references to your desired objects (this time by specifying the id) When you retrieve objects from the collector you should always cast (upcast!) them to the right type (Because objects are collected as generic Object classes) */ var btn:Button = Button(ObjectCollector.getInstance().getObject("myFirstButton")); // When/if you don't need these reference anymore // you can remove them by id: ObjectCollector.getInstance().unregisterObject("myFirstButton"); // or all together: ObjectCollector.getInstance().unregisterAll(); |
Download
You can download the class here
Documentation
A brief documentation is available here
| Print article | This entry was posted by Davide Zanotti on 20, Wednesday f, 2009 at 7:34 am, and is filed under flex. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 5 months ago
this is great! love it!