4 Replies Latest reply on Jun 23, 2010 8:48 AM by msakrejda

    why using event.parent.parent a bad practise ?

    tuliptaurus Level 1

      Hi ,

       

      I would like to know why using event.parent.parent.parent ..  in order to get to an instance a bad code practise

        • 1. Re: why using event.parent.parent a bad practise ?
          VRPDeveloper Level 3

          What do you want.. Question is not clear ...why are you using event.parent.parent.parent and what do you want to achieve please clarify

          • 2. Re: why using event.parent.parent a bad practise ?
            BhaskerChari Level 4

            Hi tuliptaurus,

             

            Accessing the way event.parent.parent is always a bad practice ...It is because you are here trying to tightly couple your application by accessing the parent components in your parent hierarchy..

             

            Well... While making some sort of simple flex/flash application, or something like that - there is no problem at all. But using such constructions (using too deep dot operator resolving and trying to control parents from within children) in at least some complicated application, where there is a need for constant updates and application extendings - you will get more and more problems with such constructions.

             

            Say if you have accessed your parent component in your application...which you are using it in many places in your application...So if by any means if you have removed your component you need to modify your code in variuos places of your application...otherwise the references will throw run time errors..

             

            So you should always follow a best practice where you need to loosely couple your components by making use of the events/custom events..

             

            Hope this may clear your doubt..

             

            Thanks,

            Bhasker Chari

            • 3. Re: why using event.parent.parent a bad practise ?
              UbuntuPenguin Level 4

              I didn't know that the event had a parent property.  But the lesson is the same , it's bad for a number of reasons.

               

              For instance , you are accessing the grandparent of the object that dispatched the event. 

               

              UIComponent A ( grandparent ) = event.parent.parent

                UIComponent B (parent )

                  UIComponent C (dispatcher)

               

              Now lets say one of your fellow coders decides to wrap UIComponent B with another group or container of some sort.  Now which container is the result of event.parent.parent ?

               

              UIComponent A (great-grandparent)  ( grandparent ) = event.parent.parent

              UIComponent FellowCodersFolly (grandparent)

                UIComponent B (parent )

                  UIComponent C (dispatcher)

               

              So if the event handling code was expecting an instance of A , it is now receiving an instance of "FellowCodersFolly".  If the components where of the same type , you wouldn't know the difference in the event-handling code , but the results may be different.

               

              Another case , lets assume your fellow coder wants to use your component somewhere else , but also retain functionality.  A common case is where you would use a component in a regular viewcomponent , then decide to use that same component in an itemrenderer.  When the event gets dispatched you will have to call Maury because you won't know who the daddy is.

               

              So in short , event.parent.parent requires that the dispatcher know something about the larger structure of the program it is in , mainly who it's grandparent is.  This breaks encapsulation , destroys reusability and can cause bugs later on when something relatively simple is changes.  Don't feel bad though , I've seen people use "event.parent.parent.parent.parent.parent.parent".

              1 person found this helpful
              • 4. Re: why using event.parent.parent a bad practise ?
                msakrejda Level 4

                I don't think Event has a 'parent' property. But, to expand on what others have said, and assuming you mean something like Event.target.parent.parent: think about the semantics of that code. "The parent of the parent of the UIComponent that dispatched this Event"--doesn't that sound awkward and brittle? As UbuntuPenguin mentioned, if your ancestor hierarchy changes, you're hosed. This *might* be okay if it's encapsulate within a single custom Component, but it's still hard to understand what you *meant* when you wrote code like that (whether it's someone else reading it, or just a future you).