7 Replies Latest reply on Jun 23, 2009 12:07 PM by maestro91

    [Noob questions] OOP and Flex Event model

    maestro91

      Hello, i have two very easy questions for an expert - I think.

       

      1) Object Oriented question

       

      If i have a

      UserElement extends Image

           UserImage extends UserElement

           UserText extends UserElement

           ...

           UserSomething extends UserElement

       

      Every UserSomething derived from UserElement has a method editElement that cannot be placed in parent class UserElement because the needs to look exactly in that leaf.

       

      Now from the Application root i have to call the editElement, but i'd prefer not being forced to have an inelegant giant switch structure ... there is a way to cast dynamically to the right class?

       

      2) Event model question

       

      I've read the documentation and googled around, but if I have this structure

       

      Application

           Canvas1

                 Control1

           Canvas2

                 Control2

       

      and i dispatch an event from Control1, the full path is Application>Canvas1>Control1>Canvas1> Application. But if I need Control2 to know that event is occoured, the only way i see is to call a method in Control2 from Application. Is this exact or there is a way to redirect the event in a broadcast way?

       

      Thankyou in advance to whoever wants to reply

        • 1. Re: [Noob questions] OOP and Flex Event model
          leybniz Level 4

          1)

           

          public class UserElement extends Image {

           

          public function editElement():void {

               // nothing here!

          }

           

          }

           

           

          public class UserImage extends UserElement {

           

          override public function editElement():void {

            // Specific code inhere

          }

           

          }

           

          public class UserText extends UserElement {

           

          override public function editElement():void {

            // Specific code inhere

          }

           

          }

           

          in the App:

          var anyUserElement:UserElement = new /* Any UserElement descendant */();

          anyUserElement.editElement();

           

           

          2)

           

          Control2 could subscribe to the events from Control1, just add appropriate event listener and you are done, the only difference is

          how to implement that

           

          If you feel this message answers your question or helps, please mark it respectively

          1 person found this helpful
          • 2. Re: [Noob questions] OOP and Flex Event model
            maestro91 Level 1

            Thank you Alexander, but now i have more questions on your aswers

             

            1)

            in the App:

            var anyUserElement:UserElement = new /* Any UserElement descendant */();

            anyUserElement.editElement();

             

             

            I think it's not exactly what i need, because the elements I have to edit are already instantiated on the descendant type, so it's non sense i my case ...?

             

            i have a var anyUserElement:UserSomething = new anyUserElement:UserSomething ();

            so i think i can't write anyUserElement.editElement();

             

            2)

             

            Control2 could subscribe to the events from Control1, just add appropriate event listener and you are done, the only difference is

            how to implement that

             

            This is not clear to me: could you write some short code please?

            • 3. Re: [Noob questions] OOP and Flex Event model
              leybniz Level 4

              1)  "i think i can't write anyUserElement.editElement();"

              why do you think so? have you tried it and failed?

              probably I'm not fully get your issue here, if so tell me more.

               

               

              2)

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">

              <mx:Script>
                <![CDATA[
                 import mx.controls.Alert;
                ]]>
              </mx:Script>

              <mx:Canvas>
              <mx:Button id="btn1" click="{btn2.dispatchEvent(new MouseEvent(MouseEvent.CLICK))}" />
              </mx:Canvas>

              <mx:Canvas>
              <mx:Button id="btn2" click="{Alert.show('Im clicked')}" />
              </mx:Canvas>

              </mx:Application>

              similar to click attribute, you could invent your own Event,

              subscribe btn2 to your custom Event from btn1

              btn1.addEventListener(YourCUSTOMEvent.TYPE, btn2.handler);

               

              Or there is another way(not perfect in terms of loose-coupling):

               

              Inside btn2 control inject:

              Application.application.addEventListener(YourCUSTOMEvent.TYPE, internalbtn2Handler);

               

              inside btn1 control inject:

              Application.application.dispatchEvent(new YourCUSTOMEvent(YourCUSTOMEvent.TYPE));

               

              this way you are using Application as event transmission medium.

               

              There are lot's of ways to go actually, just to kill the problem on the root, join both Canvases into single external component.

              1 person found this helpful
              • 4. Re: [Noob questions] OOP and Flex Event model
                maestro91 Level 1

                1) Actually have this situation:

                Canvas

                     UserSomething_1

                     ...

                     UserSomething_n

                and the n istance are already instantiated at a certain point of the runtime. Every UserSomething has a dedicated TitleWindow that works to edit some parameters of the instance to wich is associated.

                At runtime, i can't instantiate a UserElement, but i need UserSomething because of the particular extension of some variables.

                So, if i declare a protected function editElement() in UserElement and then override in UserSomething, when i try to call UserElementInstance.editElement() gives me an error.

                 

                BTW i'm managing this way:

                public function editElement(obj:Object):void {
                     var className:Class=getDefinitionByName("com.lutek.components." + obj.type) as Class;
                     className(this.getChildByName(obj.name)).showPopUp();
                }
                

                 

                passing editElement the type (the class name) and the runtime instance name of UserSomething, it works but i don't think it's the best i could have from OOP.

                 

                2) good: i didn't know i could register an event listener that way

                 

                Thank you

                • 5. Re: [Noob questions] OOP and Flex Event model
                  leybniz Level 4

                  Well, don't use 'protected' keyword, go with 'public'

                  • 6. Re: [Noob questions] OOP and Flex Event model
                    Richard_Abbott Level 3

                    maestro91,

                    to Alexander's reponse, no, protected is fine and I have used it in this way - protected is visible to inherited classes OK.

                     

                    You should be able to do:

                     

                    var a: derivedClass;

                    a = new derivedClass();

                     

                    function do_something(b: baseClass)

                    {

                      // test to find out which derived class I have been passed, then

                      ((derivedClass)b).editelement();

                      // above should call the correct method

                    }

                     

                    You need the explicit cast to ensure the right call gets pulled in, but you have used baseClass as the argument so you can send any of your derived classes over.

                    Richard

                    • 7. Re: [Noob questions] OOP and Flex Event model
                      maestro91 Level 1

                      I'm not done with OOP... btw thank you both