问题
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
.
回答1:
Yeah, this isn't a bug. This is actually a "feature" of the spark components. Basically, for SkinnableContainers (which TitleWindow is) the children are actually added to the contentGroup of the components skin. There are a few good articles explaining it all at this link and also this one. You can probably quickly change your code to the .owner property (as long as the child object is a DisplayObject) in many scenarios to achieve the result you want. A better approach might be to refactor some of your code to better fit with the view encapsulation that skins provide (have the child components fire events rather than trying to access methods on the parent... that kinda thing). it's pretty cool stuff once you understand all of the inner workings of skins though.
Hope this helps.
回答2:
It definitely changed between 3.x and 4.x. It may have gotten more exciting between the point releases too. Some details here: http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7e3a.html
来源:https://stackoverflow.com/questions/13133389/flex-parentdocument-inconsistent-between-sdk-version-4-1-and-4-5