Ok.
I partly have this working. Is this the right (or a valid) approach?
Main.mxml
//init
globalVO.addEventListener("globalChangeEvent",
globalChangeEventHandler);
private function globalChangeEventHandler(event:GlobalChangeEvent):void{
Alert.show("I heard a global change :)");
}
GlobalVO.as
//instance var
private var _evtDispatcher:EventDispatcher;
//custom add event listener
public function addEventListener(evt:String, callBack:Function):void{
this._evtDispatcher.addEventListener(evt, callBack);
}
//dispatches event
private function dispatchChangeEvent():void{
var eventObj:GlobalChangeEvent = new GlobalChangeEvent(this,
"globalChangeEvent");
this._customEvtDispatcher.dispatchEvent(eventObj);
}
This will give me the following error BUT the event is dispatched and my
main.mxml hears it.
ReferenceError: Error #1069: Property id not found on
flash.events.EventDispatcher and there is no default value.
at main/::globalChangeEventHandler()
at
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEven\
tFunction()
at flash.events::EventDispatcher/dispatchEvent()
at com.minowireless.reports.vo::GlobalVO/::dispatchChangeEvent()
at com.minowireless.reports.vo::GlobalVO/set startDate()
at main/::dispatchDate()
at main/__startDate1_change()
at
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEven\
tFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()
at mx.controls::DateField/::dropdown_changeHandler()
at
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEven\
tFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()
at mx.controls::DateChooser/::dateGrid_changeHandler()
at
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEven\
tFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()
at mx.controls::CalendarLayout/::dispatchChangeEvent()
at mx.controls::CalendarLayout/::mouseUpHandler()
Thoughts?
--- In [email protected], "phipzkillah" <[EMAIL PROTECTED]> wrote:
>
> Thanks Shaun.
>
> Can you explain this better:
>
> "or have an EventDispatcher instance variable and a custom
> method for attaching an event listener to the instance variable event
> dispatcher(know what i mean?)."
>
> It seems like you would need a public function that main.mxml can call
> to set the listener (assign it to the EventDispatcher instance
variable.
> But if the listener is set within the GlobalVO class aren't we back to
> the same issue I'm facing now?
>
> Can you provide a quick sample of what you mean? Sorry, I'm fairly
new
> to flex.
>
>
> --- In [email protected], shaun shaun@ wrote:
> >
> > Hi,
> >
> > Its probably a good idea to (re)read the dev guide section about
> working
> > with events as it may help clear this up for you. The general
problem
> is
> > that the VO is not part of the parent child hierarchy of the app so
it
> > cant bubble up the chain.
> >
> > Your globalVO needs to either (a) dispatch an event on the
application
> > itself(not a good idea) or (b) your application needs to listen to
the
> > globalVO. There are other alternatives aswell but these should be a
> help
> > to you right now.
> >
> > (a) Application.dispatchEvent("globalChange"); //or similar (in VO).
> >
> > (b) globalVO.addEventListener("globalChange",
globalVOChangeHandler);
> >
> > Option (b) will mean your globalVO will need to be an
EventDispatcher
> > subclass or have an EventDispatcher instance variable and a custom
> > method for attaching an event listener to the instance variable
event
> > dispatcher(know what i mean?).
> >
> > [snip]
> >
> > > GlobalVO.as
> > > package com.test.reports.vo
> > > {
> > > import com.test.reports.events.GlobalChangeEvent;
> > > import flash.events.EventDispatcher;
> > >
> > > public class GlobalVO
> > > {
> > > private function dispatchChangeEvent():void{
> > > var eventObj:GlobalChangeEvent = new
> > > GlobalChangeEvent(this, "globalChangeEvent");
> > > var dispatcher:EventDispatcher = new
> EventDispatcher();
> > > dispatcher.dispatchEvent(eventObj);
> > > }
> > >
> > > public function set someValue(val:String):void{
> > > dispatchChangeEvent();
> > > }
> >
> > FYI. There is a property_change event(or some similar name) that
makes
> > this pretty easy. eg)
> >
> > class Foo{
> >
> > var x:Number=0;
> >
> > funciton Foo(){
> > addEventListener(PropertyChangeEvent.propertyChange,
> > handlePropertyChange);
> > }
> >
> > function handlePropertyChange(e:PropertyChangeEvent):void{
> > if (e.property == "x" || e.property == "y"){
> > //do something.
> > }
> > }
> >
> > }
> >
> > HTH.
> > -- shaun
> >
>