[flexcoders] Re: Using dispatchEvent ?
Yes. As I said in the other post, my confusion was not understanding the relationship between the bubbling mechanism and the UI display list. I changed it back to extending EventDispatcher and dispatching off the main app object. All is well. --- In flexcoders@yahoogroups.com, Manish Jethani manish.jeth...@... wrote: On Mon, Jan 12, 2009 at 8:59 PM, biosmonkey biosmon...@... wrote: The dispatched event needs to be heard by any component, anywhere, by setting an event listener for it at the application level. So bubbling up to the app is critical. Since your component has nothing to do with UI, strictly speaking, making it a UI component (extending DisplayObject) seems like the wrong thing to do. Event bubbling is typically for UI events; your event is not a UI event, it's just that you need it to be broadcast application-wide. So I would consider making the event dispatcher object available application-wide, by making it a singleton, for instance, or by making it a property of the main application. It seems you're already doing something to that effect -- dispatching off the main application object. Manish
[flexcoders] Re: Using dispatchEvent ?
Actually, I did think about that and tried changing from EventDispatcher to extending UIComponent but it still does not work. I even tried Canvas and it still does not catch it. As an experiment, though, I tried dispatching the same event from one of my components elsewhere in the application just to make sure the custom event itself was OK, and that worked. Does it make a difference that I am dispatching this event from inside an HTTPService callback? Basically, the way this class works is that I instantiate it with a timer value, and every x seconds it makes a call to the server to get some data, and based on the result of that data I dispatch an event with the payload. --- In flexcoders@yahoogroups.com, Josh McDonald dzn...@... wrote: Event bubbling only occurs within the display list. So your dispatching object must also be a child UIComponent if you want to catch bubbled events. -Josh On Mon, Jan 12, 2009 at 4:12 PM, biosmonkey biosmon...@... wrote: I thought I had understood the event model, but apparently not. I have a custom class that extends EventDispatcher, let's call it MyClass. This class will periodically dispatch a custom event based on a timer and some condition. In my main app, I instantiate an instance of this class. I also create an event listener at the application level. Here's the problem: * If I use dispatchEvent(...) inside MyClass, the event listener in the main app never fires (the custom event *is* set to bubble) * The only way it will work is if I use the application object, ie mx.core.Application.application.dispatchEvent(...) But I don't understand why I have to do this? I have used a similar model just fine when dispatching events from inside components (ie I use dispatchEvent and my custom event is heard at the app level via bubbling). Can someone help me understand this? -- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Alternative FAQ location: https://share.acrobat.com/adc/document.do?docid=942dbdc8-e469-446f-b4cf-1e62079f6847 Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups Links -- Therefore, send not to know For whom the bell tolls. It tolls for thee. Like the cut of my jib? Check out my Flex blog! :: Josh 'G-Funk' McDonald :: 0437 221 380 :: j...@... :: http://flex.joshmcdonald.info/ :: http://twitter.com/sophistifunk
[flexcoders] Re: Using dispatchEvent ?
The dispatched event needs to be heard by any component, anywhere, by setting an event listener for it at the application level. So bubbling up to the app is critical. --- In flexcoders@yahoogroups.com, Manish Jethani manish.jeth...@... wrote: On Mon, Jan 12, 2009 at 11:42 AM, biosmonkey biosmon...@... wrote: In my main app, I instantiate an instance of this class. I also create an event listener at the application level. Here's the problem: * If I use dispatchEvent(...) inside MyClass, the event listener in the main app never fires (the custom event *is* set to bubble) To add to Josh's response, I would say the best way to do this is to listen on the object itself. obj = new MyClass(); obj.addEventListener(...); The events don't have to be bubbling then. Manish
Re: [flexcoders] Re: Using dispatchEvent ?
The event listener attached to the Application is not reacting to the event fired from your custom class because your custom class instance is not attached to the Application DisplayList. It is just an object in memory. If you want the event from your custom class instance to bubble up to the Application, you need to add it to the Application's DisplayList. The DisplayList is typically used for UI components, i.e. when you click a button in the UI, it fires an event and that event propagates up the display list. However, you can add non-UI components to it too. Your custom class just needs to extend the DisplayObject class (I think - it should have extended EventDispatcher already). You also need to add the instance to the Application object by using the addChild method on the Application object, e.g. application.addChild(myclass); If you declare the custom class instance in MXML, it is automatically added to the Application object (well, technically, only if it is a loaded component in the UI). 2009/1/12 biosmonkey biosmon...@yahoo.com: The dispatched event needs to be heard by any component, anywhere, by setting an event listener for it at the application level. So bubbling up to the app is critical. --- In flexcoders@yahoogroups.com, Manish Jethani manish.jeth...@... wrote: On Mon, Jan 12, 2009 at 11:42 AM, biosmonkey biosmon...@... wrote: In my main app, I instantiate an instance of this class. I also create an event listener at the application level. Here's the problem: * If I use dispatchEvent(...) inside MyClass, the event listener in the main app never fires (the custom event *is* set to bubble) To add to Josh's response, I would say the best way to do this is to listen on the object itself. obj = new MyClass(); obj.addEventListener(...); The events don't have to be bubbling then. Manish -- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Alternative FAQ location: https://share.acrobat.com/adc/document.do?docid=942dbdc8-e469-446f-b4cf-1e62079f6847 Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups Links
[flexcoders] Re: Using dispatchEvent ?
I see. I think the part I was missing was the fact that the class object needed to be a child of the application to be in its display list, instead of instantiated to a variable. I could convert my class to a component I suppose, and place it in my MXML but frankly I don't mind dispatching from mx..application. I just needed to understand why I had to do it that way. Thanks --- In flexcoders@yahoogroups.com, Sam Lai samuel@... wrote: The event listener attached to the Application is not reacting to the event fired from your custom class because your custom class instance is not attached to the Application DisplayList. It is just an object in memory. If you want the event from your custom class instance to bubble up to the Application, you need to add it to the Application's DisplayList. The DisplayList is typically used for UI components, i.e. when you click a button in the UI, it fires an event and that event propagates up the display list. However, you can add non-UI components to it too. Your custom class just needs to extend the DisplayObject class (I think - it should have extended EventDispatcher already). You also need to add the instance to the Application object by using the addChild method on the Application object, e.g. application.addChild(myclass); If you declare the custom class instance in MXML, it is automatically added to the Application object (well, technically, only if it is a loaded component in the UI). 2009/1/12 biosmonkey biosmon...@...: The dispatched event needs to be heard by any component, anywhere, by setting an event listener for it at the application level. So bubbling up to the app is critical. --- In flexcoders@yahoogroups.com, Manish Jethani manish.jethani@ wrote: On Mon, Jan 12, 2009 at 11:42 AM, biosmonkey biosmonkey@ wrote: In my main app, I instantiate an instance of this class. I also create an event listener at the application level. Here's the problem: * If I use dispatchEvent(...) inside MyClass, the event listener in the main app never fires (the custom event *is* set to bubble) To add to Josh's response, I would say the best way to do this is to listen on the object itself. obj = new MyClass(); obj.addEventListener(...); The events don't have to be bubbling then. Manish -- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Alternative FAQ location: https://share.acrobat.com/adc/document.do?docid=942dbdc8-e469-446f-b4cf-1e62079f6847 Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups Links
Re: [flexcoders] Re: Using dispatchEvent ?
On Mon, Jan 12, 2009 at 8:59 PM, biosmonkey biosmon...@yahoo.com wrote: The dispatched event needs to be heard by any component, anywhere, by setting an event listener for it at the application level. So bubbling up to the app is critical. Since your component has nothing to do with UI, strictly speaking, making it a UI component (extending DisplayObject) seems like the wrong thing to do. Event bubbling is typically for UI events; your event is not a UI event, it's just that you need it to be broadcast application-wide. So I would consider making the event dispatcher object available application-wide, by making it a singleton, for instance, or by making it a property of the main application. It seems you're already doing something to that effect -- dispatching off the main application object. Manish