Flex+MXML:从文本字段“输入”获取父表单事件

发布于 2024-09-30 04:15:42 字数 1512 浏览 7 评论 0原文

我不确定当尝试通过用户交互对表单“做某事”时,最佳实践是什么 - 如果这是最佳实践,我当然愿意接受替代方案。

有一个名为“enter”的 MXML 事件属性,适用于 s:TextInput,当用户在 TextInput 字段中按 Enter 键时(想象一下搜索字段 - 您输入搜索词并按 Enter),该属性会调用 AS 事件处理程序。

现在,在该事件处理程序中,我需要获取父 Form 对象。由于我们在这里讨论最佳实践,因此我对通过 ID 引用表单不感兴趣,以防我的“输入”处理程序必须能够使用不同的表单。

我的问题是 - 在 Flex 中处理事件目标的父窗体的最佳方法是什么? Flex中的父子层次结构绝对是可笑的(例如:FileSyncFB0.WindowedApplicationSkin2.Group3.contentGroup.TabNavigator7.NavigatorContent10.SkinnableContainerSkin11.contentGroup.Group17.Panel18.PanelSkin23._PanelSkin_Group1.contents.contentGroup.directoryForm_A.FormSkin32.contentGroup.FormItem34.FormItemSkin36 .contentGroup)。

严重地。

谁能理解这一点?

在上面的乱码链中,我正在寻找的对象恰好是directoryForm_A,但看看它是如何嵌套的!当然,FormItem 一定有一些属性引用其父窗体吗?

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>

TextInput 正在启动事件,并且是 event.target。按照我的 MXML 层次结构,逻辑链可能是 event.target.parent.parent 将我从 TextInput 带到 FormItem 再到 Form,但正如您所看到的,可爱的可换肤 Spark 架构将所有这些其他显示对象和容器置于其间。

是否有其他一些我可以利用的层次结构具有更语义纯的结构可供遍历?或者 FormItems 是否有内置属性或方法(至少)允许您访问其逻辑父容器?

或者我是否仍然像 Flash/AS3 开发人员一样思考太多,并且有一个不同的范式我应该穿上我的牛仔裤并采用?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅黛梨妆こ 2024-10-07 04:15:42

您不应该尝试引用 event.target.parent.parent。永远不要尝试建立这样的参考链,因为它很可能会改变,而当你改变它时,它会破坏一切。相反,为您的 TextInput 提供一个处理程序,用于处理 ENTER 事件并分派另一个(可能是自定义)事件,该事件由 event.target.parent.parent 引用的任何内容(无论是表单还是表单)监听。容器,或应用程序本身。如有必要,请设置事件气泡。然后在该处理程序中执行您想要的任何操作。

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.

感性 2024-10-07 04:15:42

永远不要尝试建立参考

为什么不呢?您只需要巧妙地做到这一点:-)

如果您知道父对象的 ID 或其类类型,您可以随时浏览显示链以获取您正在寻找的确切父对象。检查 这个为例。

针对楼主的问题:
我曾多次遇到过这种情况。我认为,通常你应该尝试避免孩子依赖父母的情况。如果您发现自己陷入了迫切需要获取父级引用的情况,则应该在对象创建时将其与子级构造函数一起传递(现在这是理论上的干净方法)。另一种方法(如上所述)是向上浏览显示列表。如果您静态地执行此操作(parent.parent.parent),那么这是危险的。上面的链接包含一个示例,显示如何使用简单的递归函数解决问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文