14 Replies Latest reply on Nov 17, 2008 12:44 AM by grkkrishna

    Global event handler for preinitialize

    pumpkin2k
      Is there anyway to add a global event handler on the components preinitialize event?

      What I need to do is to be able to enable / disable, and show / hide component dynamically based on a access list. My plan was to have a global event handler that listens to the preinitialize event, at that time, checks if the user has access to the component or not. If not, I'll disable / hide the component.

      What I did so far is:
      in my main app.mxml file, I have:
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" preinitialize="preinit(event)"
      so the preinit() function is invoked on the app's preinitialize event.

      Then I have the preinit() function (see attached code below).

      However, it does work as there is no trace message printed.
      I changed to use KeyBoardEvent.KEY_DOWN and that seems working when I press any key.

      I also tried to use the app.stage.addEventListener() but the stage object is null in the preinitialize event.

      Thanks in advance for your help.
        • 1. Re: Global event handler for preinitialize
          Gregory Lafrance Level 6
          Use a global variable, and access that global variable in the individual component's creationComplete event.
          • 2. Re: Global event handler for preinitialize
            pumpkin2k Level 1
            Thanks for the reply. However, could you elaborate your reply?

            What I was looking for is to be able to globally capture the preinitialize event on all components without have to add the
            preinitialize="myhandler()"
            statement to each component.

            And how can this be achieved by using a global variable?
            • 3. Re: Global event handler for preinitialize
              Gregory Lafrance Level 6
              You said you wanted to be able to enable / disable, and show / hide component dynamically based on an access list.

              I don't think you can do what you want, as you must add an event listener for each component.

              So the alternative is to have an access list, or more likely an access register object, that lists what components the user has access to, and then check this when the user logs in.

              Event better would be to just create roles, and assign a role to each user. When they login their role is applied, and that determines what components they can see.
              • 4. Re: Global event handler for preinitialize
                pumpkin2k Level 1
                Loading the access list is actually the easy part. The problem I have is I don't know how to dynamically hide these components once I have the access list, as the components are defined statically in the mxml files.
                • 5. Re: Global event handler for preinitialize
                  Gregory Lafrance Level 6
                  That's easy.

                  visible=false;

                  includeInLayout=false;
                  • 6. Re: Global event handler for preinitialize
                    pumpkin2k Level 1
                    Thank you for follow up on my question.

                    I do know these two properties. But that does not solve the problem. Let me explain it with an example.

                    That's say, when the user logs in, I loads the access list. Then the app displays a panel with some form entries. If I am able to register a global event listener for the component initialization events, I can check the permission and optionally hide some components. Otherwise, I would need to programmatically travels my component tree to evaluate each component individually to set there visible/includeInLayout properties, which is error prone.

                    The problem is that the component is visible to one user but would be hidden to another, so it can not be set at the design time.

                    The approach I am using right now is to extend the default components to add an event listener. But I'd like to know if it is possible to register a global event listener to be notified when any component is created. As my first post says, I tried to use the systemManager and listen to the PREINITIALIZE event, but that didn't get always get invoked.
                    • 7. Re: Global event handler for preinitialize
                      Level 7

                      "pumpkin2k" <webforumsuser@macromedia.com> wrote in message
                      news:gflpdm$7en$1@forums.macromedia.com...
                      > Thank you for follow up on my question.
                      >
                      > I do know these two properties. But that does not solve the problem. Let
                      > me
                      > explain it with an example.
                      >
                      > That's say, when the user logs in, I loads the access list. Then the app
                      > displays a panel with some form entries. If I am able to register a global
                      > event listener for the component initialization events, I can check the
                      > permission and optionally hide some components. Otherwise, I would need to
                      > programmatically travels my component tree to evaluate each component
                      > individually to set there visible/includeInLayout properties, which is
                      > error
                      > prone.

                      Why not bind those properties to the model?



                      • 8. Re: Global event handler for preinitialize
                        pumpkin2k Level 1
                        Umm.. I don't know how to bind mxml to models, still learning it.

                        Does binding to models requires any special server side setup? Because right now we are using a simple J2EE server serving requests from the flex client app.
                        • 9. Re: Global event handler for preinitialize
                          Level 7

                          "pumpkin2k" <webforumsuser@macromedia.com> wrote in message
                          news:gfnne6$q5b$1@forums.macromedia.com...
                          > Umm.. I don't know how to bind mxml to models, still learning it.
                          >
                          > Does binding to models requires any special server side setup? Because
                          > right
                          > now we are using a simple J2EE server serving requests from the flex
                          > client app.

                          No. Your model is whatever data you are using. Let's assume that you have
                          an object permissions that has a Boolean property someProperty that you are
                          using to determine if something should be visible. You could do something
                          like this:

                          <yourNameSpace:YourComponent> visible="{permissions.someProperty}"
                          includeInLayout="{permissions.someProperty}"/>

                          HTH;

                          Amy


                          • 10. Re: Global event handler for preinitialize
                            pumpkin2k Level 1
                            I see, and that data model object would be initialized based on the current user's permission. Great! Thanks for pointing this out to me!
                            • 11. Re: Global event handler for preinitialize
                              Level 7

                              "pumpkin2k" <webforumsuser@macromedia.com> wrote in message
                              news:gfolaq$3hq$1@forums.macromedia.com...
                              >I see, and that data model object would be initialized based on the current
                              >user's permission. Great! Thanks for pointing this out to me!

                              You're welcome :-)

                              -Amy


                              • 12. Global event handler for preinitialize
                                grkkrishna
                                Global event handler for preinitialize
                                • 13. Global event handler for preinitialize
                                  grkkrishna Level 1
                                  Global event handler for preinitialize problem
                                  • 14. Re: Global event handler for preinitialize
                                    grkkrishna Level 1

                                    WHEN YOU POST:
                                    - post simplified yet "complete" sample code with reduced data set