开发者

Flex+MXML: Getting parent form from textfield "enter" event

I'm not sure what the best practice is when attempting to "do something" with a form as a result of a user interaction - I'm certainly open to an alternative if it's the best practice.

There's an MXML event property called "enter" that applies开发者_高级运维 to s:TextInput which calls an AS event handler when the user presses the Enter key within a TextInput field (imagine a Search field - you type in your search term and press Enter).

Now, within that event handler, I need to get at the parent Form object. Since we're talking best practices here, I'm not interested in referring to the Form by ID, in case my "enter" handler must be able to work with different forms.

My question is - what is the best way to get a handle on the event target's parent form in Flex? The parent-child hierarchy in Flex is absolutely ludicrous (eg: FileSyncFB0.WindowedApplicationSkin2.Group3.contentGroup.TabNavigator7.NavigatorContent10.SkinnableContainerSkin11.contentGroup.Group17.Panel18.PanelSkin23._PanelSkin_Group1.contents.contentGroup.directoryForm_A.FormSkin32.contentGroup.FormItem34.FormItemSkin36.contentGroup).

Seriously.

Who can make any sense of that?

In the chain of gibberish above, the object I'm looking for happens to be directoryForm_A but look just how nested it is! Surely there must be some property of a FormItem that refers to its parent form?

The MXML structure is much more meaningful semantically:
<s:Form id="directoryForm_A" width="100%">
 <s:FormItem width="100%" label="URI">
  <s:layout>
   <s:BasicLayout/>
  </s:layout>
  <s:helpContent>
   <s:Label text="Help String"></s:Label>
  </s:helpContent>
  <s:TextInput left="0" right="0" enter="handleUserSetRootDirectory(event)"/>
 </s:FormItem>
</s:Form>

The TextInput is initiating the event, and is the event.target. Following my MXML hierarchy, a logical chain might be event.target.parent.parent taking me from TextInput to FormItem to Form, but as you can see, the lovely skinnable Spark architecture puts all these other display objects and containers in-between.

Is there some other hierarchy I can exploit that has a much more semantically pure structure to traverse? Or is there a built in property or method of FormItems (at least) that allow you to access their logical parent containers?

Or am I still thinking too much like a Flash/AS3 developer and there's a different paradigm I should just hike up my jeans an adopt?


You shouldn't be trying to refer to event.target.parent.parent. Never try to establish a reference chain like that, since it is likely to change and when you do change it it will break things. Instead, have a handler for your TextInput that handles the ENTER event and dispatches another (perhaps custom) event that is listened for by whatever event.target.parent.parent refers to, be it a form, a container, or the application itself. If necessary, have the event bubble. Then do whatever you want in that handler.


never try to establish a refernce chain

Why not? You just have to do it cleverly :-)

If you know the parent's ID or it's Class type you can always browse thru the display chain to get the exact parent object that you are looking for. Check this for an example.

To the poster's problem: I ran into this quite a few times with spark. I think, usually you should try avoit situations where the child is dependant of it's parent. If you find yourself trapped in a situation where you desperately need to get a reference of the parent, you should pass it with the child's constructor on object creation (now that's the clean way - in theory). Another way (as decribed above) is to walk up through the display list. Wich is dangerous if you do it statically (parent.parent.parent). The link above contains an example that shows, how to solve the problem using a simple recursive function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜