The MXMLLoader Class > Dynamic load MXML In Flex Builder 3
First of all I don’t really use flex to do flex stuff and I’m not a big fan of flex. But in my latest project I had to build with flex an interface witch would be always diferent (a dynamic interface). In the beginning we thought in parsing some type of XML, but if we are in flex why not parse MXML? I took a look over the internet and I only found MXMLLoader component v0.01, witch didn’t compile on flex builder 3 and was really in the beginning. So there it is, as XMMLLoader class for flex builder 3. You just have to instantiate the class with the mxml :
You can access objects by name:
Or by id:
And have access to component events like:
The class with an example:
May 14th, 2008 at 8:18 pm
Thank you for this article. I’ve been playing around with your example as well as the original component that you mention and I seem to be stuck. Are you familiar with the following error and where I may have gone wrong?
Thanks.
Error #1009:
[code]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at MXMLLoader/createChildFromMXMLNode()[/Users/bealtr/Documents/Storage/Workspace/Flex Builder 3/MXML/src/MXMLLoader.as:111]
at MXMLLoader()[/Users/bealtr/Documents/Storage/Workspace/Flex Builder 3/MXML/src/MXMLLoader.as:41]
at MXML/init/onLoaded()[/Users/bealtr/Documents/Storage/Workspace/Flex Builder 3/MXML/src/MXML.mxml:25]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
[/code]
Here’s the code for my main mxml file. I’ve got both MXMLLoader.as and MXMLManifest.as in the source directory.
[code]
[/code]
May 14th, 2008 at 8:19 pm
Oops. I guess it filtered the code from my example is there a way for me to post you the code?
May 14th, 2008 at 8:21 pm
could you please post the files online so that we have a look?
It would generally seem as accessing it too early, or a typo, but do post the code
May 14th, 2008 at 10:14 pm
June 18th, 2008 at 3:00 pm
Is it possible to use DataGrid components? When trying I receive errors like:
TypeError: Error #1034: Type Coercion failed: cannot convert mx.controls::DataGrid@151c7851 to mx.core.Container.
and
ReferenceError: Error #1065: Variable is not defined.
What you are showing here seems very powerful!
June 18th, 2008 at 4:46 pm
June 18th, 2008 at 5:33 pm
That is what I was thinking…thank you for the verification and thank you for sharing your insight and example.
September 10th, 2008 at 7:19 pm
Very nice addition to the original work. Brilliant way of handling events for the components. Has anyone tried passing parameters from the created components? I was able to use e.currentTarget.label to pass information from a button back to the main application, but this is a sorely limiting hack. Any ideas?
September 27th, 2008 at 9:41 pm
Nice piece of work. The example is easy to follow and I got it working in a matter of minutes.
I tried to do this:
var mxmlLoader:MXMLLoader = new MXMLLoader(mxml,this);
this.removeChild(mxmlLoader);
naively assuming the mxmlLoader (extended from UIComponent) had been added as a child to “this.” A look at the source makes it clear that it’s the components within the xml that are added as children and that the structure is maintained (i.e., you can remove everything by removing an uber-container that held all the components).
Architecturally, an alternative to the approach of providing both the conversion and adding the child in one step would be to return the objects of type UIComponent which could be added when and where appropriate.
As the author notes, not all components and attributes are handled.
I am working on something where I need to receive dynamically a relatively small, simple piece of Flex code without any fore knowledge and execute it within the context of my app. MXMLLoader works fine for the visual pieces and while Flex is able to bind “click” attributes to functions outside the MXMLLoader source, I need something equivalent to , which appears to be way outside of the approach that MXMLLoader takes using the manifest and getDefinitionByName(), etc.
Thoughts anyone? And thanks again to the author for a nicely done and nicely presented piece of code.
September 27th, 2008 at 9:45 pm
In the next to last paragraph, the posting removed a reference to the Script/CDATA mechanism for including Actionscript source.