Flex 4,自定义组件的多个实例监听父级的同一事件

发布于 2024-10-14 18:17:09 字数 617 浏览 3 评论 0原文

简而言之:

我需要在自定义组件中使用事件侦听器,以便其所有实例(无需编辑它们)同时做出反应,并由其父容器中的分派事件触发。

详细信息:

  • 我有一个带有选项卡导航器的自定义组件。 (这些选项卡旨在显示不同语言的不同首选项。)

  • 我有一个包含所有语言按钮的按钮栏。

  • 有很多自定义组件的实例。

我想单击语言栏的按钮并将所有实例切换到同一选项卡(自定义组件包含更改选项卡的逻辑)。

我可以通过为自定义组件的每个实例添加事件侦听器来完成此操作,因此它调用更改选项卡的内部函数。但看起来耦合度很高,不是吗?

我想知道是否可以在组件的主类中完成,因此它监听其父容器中的事件,无论它是什么。

在我看来,这段代码应该可以工作,但它不能(显然会使用自定义事件来传递新的语言值):

this.parent.addEventListener("lang_change", this.change_tab);

这样我就可以删除组件的一个实例,然后看到它为自己工作。

先感谢您

In short:

I need an event listener in a custom component so all its instances (without editing them) react at the same time, fired by a dispatched event in its parent container.

In detail:

  • I have a custom component with Tab navigator. (The tabs are intended to show different preferences for different Languages.)

  • I have a button bar with buttons for all the languages.

  • There are a lot of instances of the custom component.

I want to click in a button of the languages bar and get ALL the instances switched to the same tab (the custom component contains the logic to change the tab).

I can do it by adding the event listener for EACH INSTANCE of the custom component, so it calls an internal function that changes the tab. But it seems to be very coupled, isn't it?

I wonder if it can be done in the master CLASS of the component, so it listen for events in its parent container, whichever it is.

In my mind this code shoud work, but it doesn´t (obviously ill'use a custom event to pass the new language value):

this.parent.addEventListener("lang_change", this.change_tab);

This way I can just drop an instance of the component, and see it working for itself.

Thank you in advance

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

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

发布评论

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

评论(2

数理化全能战士 2024-10-21 18:17:09

我需要一个自定义的事件监听器
组件所以它的所有实例
(不编辑它们)在
同时,由调度事件触发
在其父容器中。

根据定义,您想做的事情就是破坏封装。在理想的世界中,组件应该不知道它的父组件。如果组件需要与其父组件通信,则应该调度一个事件。如果父级需要与子级通信,则应调用该子级的公共方法(或更改公共属性)。从封装的角度来看,我不建议子进程监听父进程的事件。

我想点击一个按钮
语言栏并获得所有
实例切换到同一选项卡
(自定义组件包含
更改选项卡的逻辑)。

因此,然后为按钮放置一个单击处理程序并执行如下操作:

public function onClick():void{
  myCustomTabNavigator1.selectedIndex = 1 
  myCustomTabNavigator2.selectedIndex = 1 
  myCustomTabNavigator2.selectedIndex = 1 
}

如果您引用了 selectedItem,则还可以设置它。 ,如果您的自定义 TabNavigators 位于数组中,则可以循环它们。如果自定义 TabNavigators 是自定义组件的子级,您可以在该自定义组件中创建一个方法来设置默认值,并在每个组件上调用该方法,而不是直接设置 selectedIndex。

I need an event listener in a custom
component so all its instances
(without editing them) react at the
same time, fired by a dispatched event
in its parent container.

The very thing you want to do, by definition, breaks encapsulation. In an ideal world, a component should know nothing of it's parent. If the component needs to communicate with it's parent, it should dispatch an event. IF a parent needs to communicate to children it should call a public method on that child (or change a public property). From an encapsulation stand point, I cannot recommend that the child listen for events on the parent.

I want to click in a button of the
languages bar and get ALL the
instances switched to the same tab
(the custom component contains the
logic to change the tab).

So, then put a click handler for the button and do something like this:

public function onClick():void{
  myCustomTabNavigator1.selectedIndex = 1 
  myCustomTabNavigator2.selectedIndex = 1 
  myCustomTabNavigator2.selectedIndex = 1 
}

You can also set the selectedItem if you a reference to it. , If you have your custom TabNavigators in an Array, you can loop over them. IF the custom TabNavigators are child of your custom component you can create a method in that custom component to set the defaults and call that method on each component instead of setting selectedIndex directly.

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