English 中文(简体)
Flex 4 Bubbling custom event
原标题:

How to create a bubbling custom event in Flex 4?

To create and expose a custom event in MXML, you need to declare it at the component that will dispatch the event with this line:

<fx:Metadata>
        [Event(name="select", type="my.engine.events.SelectionEvent")]
</fx:Metadata>

This allows you to:

<my:CustomComponent select="doSomething()"/>

However, how do you make this bubble upwards. I want to do this

<s:DataGroup select="doSomethingForAll();">
   <s:itemRenderer>
      <fx:Component>
         <my:CustomComponent/>
      </fx:Component>
   </s:itemRenderer>
</s:DataGroup/>

Thanks!

问题回答

Your Custom event must extend Event. On constructor you ll find name:string, bubbling:boolean, and cacellable:boolean as arguments.

Set the bubbling parameter to true. In your example, the metadata tag must be in your DataGroup tag.

One possible solution but not exactly what i was looking for is to add this line of code at the DataGroup level.

this.addEventListener(SelectionEvent.SELECTED, onSelect);

This will ensure all events fired by CustomComponent is cought.

You can either extend s:DataGroup container with specified custom metatag data information built-in into the extended class or you can either call "doSomethingForAll()" method from itemRenderer s "select" event handler, see code below:

<s:DataGroup         
    dataProvider="{instructions}"        
    width="100%">        
    <s:itemRenderer>
        <fx:Component>
            <my:CustomComponent                    
                select="rendererSelect()">
                <fx:Script>
                    <![CDATA[

                        protected function rendererSelect():void
                        {
                            outerDocument.doSomethingForAll();
                        }

                    ]]>
                </fx:Script>
            </my:CustomComponent>
        </fx:Component>
    </s:itemRenderer>                
</s:DataGroup> 

Catch the dataGroups select event and then dispatch a doSomethingForAll()

Make sure the doSomethingForAll event has it s bubbling property set to true.

Then any event listeners listening for doSomethingForAll above it in the display list will get called.





相关问题
Mysql trigger/events vs Cronjob

I have an auction website which let my users place an unlimited amount of autobiddings. To monitor these autobiddings something has to check the database every second. My question is if it is ...

Can an event be used as an event listener?

I m trying to expose some events from a private object which is contained inside the object I am creating and it looks like the compiler is happy with this: private WindowUpdateServer ...

鸡奸

由于将javascript DOM方法放在第html页底部(在<有人>之后)远比利用“j Query”准备活动要快得多,因此我们不得不这样做:

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

jQuery bind on ajax load() event

I have a page which displays multiple blocks with results details. Inside each block I have some <a> tags with thickbox jQuery plugin attached: class="thickbox" Here is an example of one kind ...

HTML text input event

I have a form, and want to disable/enable its submit button depending on whether the form s input text fields are empty or have had text entered into them. I think this means having an event handler ...

IE doesn t run Javascript when you click back button

I ve built a shop with tons of JS running. One of the pieces of the cart, is a zip code field which is required to calculate shipping cost. This works fine going through the cart. Now clicking ...

热门标签