Next time I have allergies and hit the nasal spray, I’ll end up using Internet Explorer and install the Yahoo! Toolbar…
…NOT!
Hormone Appears to Increase Trust
Via her majesty.
Next time I have allergies and hit the nasal spray, I’ll end up using Internet Explorer and install the Yahoo! Toolbar…
…NOT!
Hormone Appears to Increase Trust
Via her majesty.
I managed to get DataSelector to work earlier this week and wanted to share how cool it is once you get it working.
Introduction
DataSelector is a mixin class in the Flash MX 2004 Professional v2 component framework (hereafter referred to as the v2 framework). It’s full classpath is:
mx.controls.listclasses.DataSelector
It is used to give a UIComponent the core attributes of working with the common data methods and properties that are prevalent in the v2 framework.
So, if you want your component to have all of the dataProvider methods like addItem, removeItemAt (to interact with a dataProvider thus implementing the DataProvider API), as well as the useful properties to query the View (your UIComponent) such as selectedItem, selectedIndex, and value, this is the class to use. It also gives your UIComponent a dataProvider property, and when it changes, calls invalidate for you. There are a plethora of functions and properties given to you to make more effecient redraws when the model does change.
Why should I care?
Why would you use this? I like it because I don’t feel like proxying all of the DataProvider API calls myself; I can just use the DataSelector, and it automatically gives my class all of those methods instead of me having to write them manually. Secondly, all of the fine grained control over how a dataProvider’s data is changed, as well as keeping track of which item is selected, etc., is all handled for you.
Inheritance vs. Mixin
Why can’t you just inherit from it? As I said, it’s a mixin. Since ActionScript 2.0 does not support multiple inerhitance, you must either use an Interface, Composition, __resolve, or a mixin. A mixin is basically a Decorator that at runtime adds methods at properties to a class’s prototype. Since ActionScript 2.0 supports prototype, this means anything added on a class’ prototype is effectively the same thing as a class member variable or class function.
It manages to compile correctly because a plethora of the base classes of the v2 framework define properties & methods (and the DataSelector template) to ensure the compiler finds the functions and properties that will be there at runtime. So, even though no dataProvider exists in your code at authortime, it will at runtime, so you have to define it so the compiler doesn’t bitch.
Implementing
DataSelector comes with a template that defines all of these properties and methods for you so you can just copy and paste it into your class file. The only two things you have to do to use it are:
Like so:
import mx.core.UIComponent; import mx.controls.listclasses.DataSelector; import mx.controls.listclasses.DataProvider; class YourClass extends UIComponent { // your DataSelector template will go here public static var mixIt:Boolean = DataSelector.Initialize(YourClass); public static var mixIt2:Boolean = DataProvider.Initialize(Array); }
Then, you can just react to any data changes in your draw/size/doLayout function (doLayout assuming you extend View instead of UIComponent).
createClassObject(YourClass, "test", 0); test.dataProvider = ["one", "two", "three"]; test.addItem({label: "four", data: 4}); test.selectedIndex = 0; trace(test.selectedItem);
Conclusion
Now, in all fairness some MVC purists will say that this is a violation since this allows your View to be bound to the Model via the dataProvider, which is itself a controversial way of handling data in Flash. I argue that the Controller is the one who bound them so it’s legal. Additionally, in MVP, a modelChanged event is triggered upon using a DataProvider API function like addItem, but the View gets the event and reacts, not the Presenter. Anwyay, I like it and it helps my custom components integrate nicely in the v2 framework.
Editing my bio over at OpenARP, and noticed Aral’s using a Flash movie for a Captcha test in his Wysiwyg Wiki. This is the 2nd time I’ve seen Flash used in this manner.
I use it for my comments so blog spambots cannot directly hit my mt-comments.cgi without knowing the key. Since the key is just a variable hidden in my SWF, and no blogspammer has yet written a Perl spambot to rip open my SWF, introspect the variables, and thus carry this key to my server… I remain protected from blog spam.
I’ve noticed over the past 3 years, Captcha’s have gotten more and more elaborate. The one used when signing up for an Microsoft Passport, used in Windows Messenger, is nigh unreadable (even after 5 page refreshes), and I’ve got 20/20 vision.
Aral’s using it to confirm a human is in fact making an edit to the Wiki just by a simple animation (a lot more readable than the Salvador Dali’s I’ve seen generated). Smart thinking, Aral! Another great niche use of Flash.
Dan Glauser, Leif Wells and I will presenting at the all day Java DevCon 2005 this Wednesday. Leif and I personally won’t be there all day as we have work to complete, but will we answer questions, group & individually.
Leif and I will be showing how Flex is the phatest presentation tier ever invented. We’ll be going over what we feel the enterprise Java community wants and needs to see based on reactions from the last AJUG meeting, my readings from community lists, and from talking with various developers. Case in point:
Dan will take off in the 2nd half where Leif and I leave off, filling in the technical gaps from a Java developer’s perspective, describing what is going on in the backend.
If you’ve got questions about Flex, please come and talk to one of us. I personally love to chat about technology, want to spread & describe the coolness that is Flex, and look forward to learning from others in the field.
Hope to see you there!