开发者

addEventListener is not working when dispatchEvent is invoked in a model (MVC) AS3

I have a problem where I dispatch an event in a model class which is dispatched correctly. When trying to listen to this in the client, nothing is listened to. Client.as instantiates all MVC elements such as:

var _biosModel:IMultiDataModel = new BiosPanelModel();

A user click invokes an 'update' method within 'BiosPanelModel.as' like so:

override public function update():void
{
    debug("[BiosPanelModel.update]");
    dispatchEvent(new Event(Event.CHANGE)); // dispatch event
}

Client.as then tries to listen to this dispatch开发者_高级运维ed event but doesn't and I don't know why!? like so:

this._biosModel.addEventListener(Event.CHANGE, eventHandler);

eventHandler is not called!

Hope someone can help me as I feel like this should be simple! :-(

Thanks for reading

Chris


I would start by trying to set the bubbles parameter to true. So, dispatch the event like so:

dispatchEvent(new Event(Event.CHANGE, true));

This will keep you from having to listen to the event explicitly on the dispatcher and will allow the event to be captured in the bubbling phase.

EDIT
This IS NOT the end-all, be-all answer... and you will likely want to remove the bubbles = True once you get it working correctly, unless you want to explicitly cancel the event after you capture it. The reason being is the Event.CHANGE event is a pretty common one, and bubbling that event up the Display List chain could be messy, especially if there are other handlers listening for CHANGE events. But, it WILL let you know whether you are attaching your listener correctly.
END EDIT

The other thing you might want to try (since it is not apparent from the way you have your code written above) is to add the event listener immediately after creation of the instance. Alse, make sure your _biosModel is an instance property, not defined within the scope of some function that generates it.

package ... {

    import flash.events.Event;    

    public class Something extends SomethingElse {

        var _biosModel:IMultiDataModel;

        public function Something() {
            this._biosModel = new BiosPanelModel();
            this._biosModel.addEventListener(Event.CHANGE, __eventHandler);
        }

        private function __eventHandler($evt:Event) {
            trace("SUCCESS");
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜