3 Replies Latest reply on Aug 20, 2009 1:31 AM by gibbetnet

    Basic problems with extension of IEventDispatcher in application

    gibbetnet

      Hi at All!

      I’ve got a general problem to create a MVC-Pattern, because the FlexBuilder throw an error message in the “Problems”-Box when I try to add an eventlistener to the model.  I do not understand why it is so and hope, that somebody could explain me what’s wrong in my code. The error message is

      “1195: Versuchter Aufruf der nicht verfügbaren Methode addEventListener über einen Verweis mit statischem Typ MainFile”,

      which means, that I tried to call a not available method addEventListener via a reference of static type MainFile. In the following code I give a short exampl, which generated the error message in the FlexBuilder.

      The basic Interface is a very simple extension of the IEventDispatcher. The Code is:

      package model.interfaces

      {

            import flash.events.IEventDispatcher;

       

            public interface IMapModel extends IEventDispatcher

            {

                  function getKey(node:XML):String;

            }

      }

      The MapModel class extends EventDispatcher and implements the IMapModel. The Code is:

      package model

      {

            import flash.events.EventDispatcher;

            import flash.events.Event;

            import model.interfaces.IMapModel;

            import flash.events.IEventDispatcher;

       

            public class MapModel extends EventDispatcher implements IMapModel

            {

                  public function MapModel(target:IEventDispatcher=null)

                  {

                        super(target);

                  }

                 

                  public function getKey(node:XML):String

                  {

                        return "Key";

                  }

            }

      }

      In the MainFile, which is an mxml-Application, I get the error-message in the init-method. The Code here is:

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

           

            <mx:Button label="TEST"  x="402" y="151" click="this.test()"/>

            <mx:Script>

                  <![CDATA[

                        import mx.controls.Alert;

                        import model.MapModel;

                        import model.interfaces.IMapModel;

                       

                        private var model:IMapModel;

                       

                        private function test():void

                        {

                              this.model = new MapModel();

                              this.model.addEventListener(Event.CHANGE,this.testAlert);

                              this.model.dispatchEvent(new Event(Event.CHANGE));

                        }

                       

                        private function testAlert(e:Event):void

                        {

                              Alert.show("TEST");

                        }

                  ]]>

            </mx:Script>

      </mx:Application>

      When I start to run the application FlexBuilder tell me, that an error exist in required project and ask, if it proceed with launch. When I proceed, all works fine (which means that the Alert occurs after clicking the Test-Button) in spite of the warning from FlexBuilder. So.. why it warning??? Whats wrong? It is really a miracle for me…

      By the way:

      When you replacing the lines

      this.model.addEventListener(Event.CHANGE,this.testAlert);

      this.model.dispatchEvent(new Event(Event.CHANGE));

                       

      in the init-method by the following lines:

       

      (this.model as IEventDispatcher).addEventListener(Event.CHANGE,this.testAlert);

      (this.model as IEventDispatcher).dispatchEvent(new Event(Event.CHANGE));

       

      no more error message occurs in the FlexBuilder. But… why I have to declare explicit this.model as IEventDispatcher when it is a IMapModel (which extend the IEventDispatcher)??? Why it does not inherit the methods of the IEventDispatcher directly???

      Thanks for reading this long description ;o),

      Isabel