10 Replies Latest reply on Dec 22, 2010 9:26 AM by rtalton

    Passing values to variables of a custom component at runtime.

    linrsvp Level 1

      Hi,

       

      I have 2 custom components within my parent application.

       

      In the first component I have a datagrid. When I click on a row of the datagrid, I want to pass an id from this custom component to the second custom component. I tried doing this in the click event of the dg :

       

      The id of the second component is "ticketZoomComponent". This component is only included in the "TicketZoom" state.

       

      protected function dg_clickHandler(event:MouseEvent):void
                  {   
                      FlexGlobals.topLevelApplication.ticketZoomComponent.TicketId = dg.selectedItem.ticket_id;
                     
                      FlexGlobals.topLevelApplication.currentState = 'TicketZoom';
                  }

       

      But when i click, it throws an error : "Cannot access a property or method of a null object reference."

      Does it mean that the component does not exist in the main application since its included only in that state ?

       

      What am I doing wrong ?

        • 1. Re: Passing values to variables of a custom component at runtime.
          Claudiu Ursica Level 4

          Use the debugger and see if you actually have a value for

          FlexGlobals.topLevelApplication.ticketZoomComponent. Something is null in there

          hence the error message.

           

          C

          • 2. Re: Passing values to variables of a custom component at runtime.
            linrsvp Level 1

            Yes when I debugged it, I see that the value of my component is null  ( ticketZoomComponent value is null).

             

            Does that mean that a component is null until the application enters the state in which it is included ?In that case, how do I pass values to my custom component before changing the state ? Please help....!!

            • 3. Re: Passing values to variables of a custom component at runtime.
              saisri2k2 Level 4

              Yes, the component will be null. in order to figure out, you need to handle creationComplete on the second component, if that creation complete is never called and you are trying to post something to the component then it wont work

               

              do something like

               

              if(FlexGlobals.topLevelApplication.icketZoomComponent){
                   FlexGlobals.topLevelApplication.ticketZoomComponent.TicketId = dg.selectedItem.ticket_id;
                   FlexGlobals.topLevelApplication.currentState = 'TicketZoom'; 
              }else{
                   FlexGlobals.topLevelApplication.currentState = 'TicketZoom';
                   FlexGlobals.topLevelApplication.addEventListener(StateChangeEvent.onstatechange,someFunctionhandler);
              }
              
              //---- some function
              function void someFunctionhandler(evt:StateChangeEvent):void{
                   FlexGlobals.topLevelApplication.ticketZoomComponent.TicketId = dg.selectedItem.ticket_id;
                   FlexGlobals.topLevelApplication.removeEventListener(StateChangeEvent.onstatechange,someFunctionhandler);
              }
              
              
              • 4. Re: Passing values to variables of a custom component at runtime.
                SashaKeith Adobe Employee

                How about using a Singleton?

                • 5. Re: Passing values to variables of a custom component at runtime.
                  linrsvp Level 1

                  That dsnt help. The problem is, when the application changes its state to ticketZoom, its TicketID value should be set. Thats the only way it can display data. In this case, we are changing the state, at which time the TicketID value is still null. After the view is rendered, the functionHandler function sets the value of ticketID.

                   

                  I want the ticketID value to be set before the view is rendered ...

                   

                  @Sasha : could you elaborate more on that ? (Singleton)

                  • 6. Re: Passing values to variables of a custom component at runtime.
                    Claudiu Ursica Level 4

                    Am i misreading you or you are trying to do something like display a detail for

                    the selected item ?

                    C

                    • 7. Re: Passing values to variables of a custom component at runtime.
                      BhaskerChari Level 4

                      Hi linrsvp,

                       

                      Try to listen for the StateChangeEvent.CURRENT_STATE_CHANGING instead of  StateChangeEvent.CURRENT_STATE_CHANGE  there is a small difference between two...StateChangeEvent.CURRENT_STATE_CHANGING is dispatched when the state is about to change but during this time the state is not yet changed where as StateChangeEvent.CURRENT_STATE_CHANGE is dispatched when the currentState of application is just changed and during this time your app currentState is already changed and which you dont want so listen for StateChangeEvent.CURRENT_STATE_CHANGING event and set the TicketId  as shown below:

                       

                       

                      if(FlexGlobals.topLevelApplication.icketZoomComponent){
                           FlexGlobals.topLevelApplication.ticketZoomComponent.TicketId = dg.selectedItem.ticket_id;
                           FlexGlobals.topLevelApplication.currentState = 'TicketZoom';
                      }else{    
                           FlexGlobals.topLevelApplication.addEventListener(StateChangeEvent.CURRENT_STATE_CHANGING ,onCurrentStateChanging);

                           FlexGlobals.topLevelApplication.currentState = 'TicketZoom';
                      }

                       

                      function void onCurrentStateChanging(evt:StateChangeEvent):void{
                           FlexGlobals.topLevelApplication.ticketZoomComponent.TicketId = dg.selectedItem.ticket_id;
                           FlexGlobals.topLevelApplication.removeEventListener(StateChangeEvent.CURRENT_STATE_CHANGING,onCurrentStateChanging);
                      }

                      If onCurrentStateChanging function is not being called incase then try to register the StateChangeEvent.CURRENT_STATE_CHANGING addEventListner in the application creationComplete instead as above.

                       

                      Thanks,

                      Bhasker

                       

                      Message was edited by: BhaskerChari

                      • 8. Re: Passing values to variables of a custom component at runtime.
                        linrsvp Level 1

                        Hi ,

                        Thanks for the detailed explanation. However I tried that too, but the value of TicketID dsnt get updated

                        • 9. Re: Passing values to variables of a custom component at runtime.
                          venkatsnet

                          Always use getter/setter methods and commit properties to update properties of UI components

                           

                          override protected function commitProperties():void {

                          private var propertyChanged:Boolean = false;

                          private var _text:String = "":

                          super.commitProperties();

                          if(propertyChanged) {

                          propertyChanged = false;

                          textLabel.text =_text;

                          }

                          }

                           

                          public function set text (value:String):void {

                          _text = value;

                          propertyChanged = true;

                          invalidateProperties();

                           

                          }

                          • 10. Re: Passing values to variables of a custom component at runtime.
                            rtalton Level 4

                            If you don't want to use a singleton to store data, here's an example of using a setter/getter and letting Flex data binding update the child component automatically after it is instantiated and data is available. This way you don't have to worry if the component is null; Flex takes care of that for you. You will not get a null error.

                             

                            In main app:
                            <!-- bind datagrid's selectedItem data to the setter function in child component -->
                            <custom:ticketZoomComponent: ticket_id ="{dg.selectedItem.ticket_id}" />

                            <!-- NOTE: the setter function is named "ticket_id" -->


                            In the ticketZoomComponent (child component):

                            //private var used only within this component:
                            [Bindable]
                            private var _ticket_id:String;

                            //setter & getter:
                            public function set ticket_id(ticketID:String):void {
                                  if (ticketID != null{
                                   _ticket_id = ticketID;
                                   //do any other UI changes here... it is now safe as you know you have the data!
                                  }
                            }

                             

                            public function get ticket_id():String{
                                 return _ticket_id;
                            }

                             

                            <!-- display the data in the UI using the private var-->
                            <mx:Label id="lblTicketID" text="{_ticket_id}"/>