I'm seeing some inconsistencies with parentDocument
between Flex SDK 4.1 and 4.5. In a nutshell, existing code that works in 4.1 is broken in 4.5 (and 4.6). Here's a small example to demonstrate:
TestProject.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="500" minHeight="500" xmlns:local="*">
<local:SubComponent x="50" y="50"/>
</s:Application>
SubComponent.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="200" height="200">
<fx:Script>
<![CDATA[
import spark.components.Application;
protected function button1_clickHandler(event:MouseEvent):void
{
var app:Application = this.parentDocument as Application;
if (app != null) {
trace('Good');
} else {
trace('Bug');
}
}
]]>
</fx:Script>
<s:Button x="18" y="20" label="Button" click="button1_clickHandler(event)"/>
</s:Group>
With the 4.1 SDK, I'm successfully able to retrieve the Application
object via parentDocument
. However, this fails with the 4.5 SDK. With 4.5, parentDocument
is now a skin object:
"parentDocument" spark.skins.spark.ApplicationSkin (@9d7e479)
If I go up two levels, then I successfully get the Application object:
"parentDocument.parentDocument" TestProject (@9d7e0a1)
So, I could work around the problem by replacing parentDocument
with parentDocument.parentDocument
. However, I'm not clear whether this change is expected behavior that I can count on being consistent going forward, or if it's a bug that will likely be fixed sometime in the future, at which point the behavior will revert to the 4.1 way of doing things. So, my questions are:
- Has anyone else run into this issue? If so, how did you handle it?
- If you have developed a workaround, does your solution depend on a specific SDK version, or does it work for all SDK versions?
- Was the change in behavior from 4.1 to 4.5 intentional, or is this a bug? I suspect bug, but I've been unable to find anything definitive one way or the other.
Note that my example above is contrived to demonstrate the problem with the least amount of code and minimal complexity. In my actual project, I have a module that contains a TitleWindow
with subcomponents, and I'm trying to have the subcomponents call methods in the TitleWindow
(via parentDocument
). So, workarounds that involve other ways of finding the Application
object aren't really applicable because the subcomponents are actually looking for a TitleWindow
, not the Application
.