By Derek Santos – Consultant
This hidden gem in Flex can really help keep your MXML code clean and separated from your view logic. A great way to use it is to encapsulate your view logic into a ViewHelper. IMXMLObject has only one function.
Here is Adobe’s documentation for this function:
“Called after the implementing object has been created and all component properties specified on the MXML tag have been initialized.”
What this means is that once the parent is created and has intialized its properties, it will pass a reference of the parent to your IMXMLObject implementation. So, as an example, let’s create a simple ViewHelper. First, create a class and implement IMXMLObject.
Notice that I am keeping a reference of the view by assigning the value of document to an instance variable called view. Once you do this, you can work with your view and apply any logic you may need.
Once you’ve created your ViewHelper, you simply need to declare it in your MXML file. In this case, that would be MyView.
<local:ViewHelper id="helper" />
Notice that you do not need to pass a reference to “this” anywhere. This keeps your MXML very clean and makes the ViewHelper a little more portable. Comments are welcome!