20 Replies Latest reply on Jul 28, 2010 9:59 AM by ArjanDs

    refresh component each time it becomes visible

    ArjanDs

      This question is probably not the first time its being asked, but search results werent much of a help.

       

      in the Main.mxml i have two buttons showing (or hiding) custom components.

       

      the first time the components are loaded/showed they use the creationComplete event with lets call it init() method. (a service/CallResponder call to populate certain panels/labels etc)

       

      during the switching of and on of the components the information in the database might have changed and i therefor need to recall the init() method again to restart the service/CallResponder again. So i always have the most acurate info displayed on my screen.

       

      is there a way to invoke the init() method each time the custom component becomes visible?

       

      thanks in advance

      arjan

        • 1. Re: refresh component each time it becomes visible
          rtalton Level 4

          If your components are based on UIComponent, just use the show/hide events to issue a call to your init() method.These events are triggered when the component is toggles between visible/not visible.

          • 2. Re: refresh component each time it becomes visible
            ArjanDs Level 1

            ok, so it works ONLY with <mx:UIComponent> ?

             

            my components are based on groups and have multiple states as well, any suggestion for that?

            • 3. Re: refresh component each time it becomes visible
              rtalton Level 4

              I use FB3, so perhaps someone else can answer that one.

              Look in your help docs and see what capabilities "group" has, if it has show/hide events.

              • 4. Re: refresh component each time it becomes visible
                ArjanDs Level 1

                group has the same capabilities of show/hide as in FB3

                show

                Event
                Event Object Type: mx.events.FlexEvent
                property FlexEvent.type = mx.events.FlexEvent.SHOW
                Language Version: ActionScript 3.0
                Product Version: Flex 3
                Runtime Versions: Flash Player 9, AIR 1.1
                so you can help anyway

                 

                 

                ok so i have this in my Main.mxml

                 

                    <local:DomainView id="domainView"
                                         regentid="{regentid}"
                                         visible="{loggedIn}"
                                         includeIn="domainInfo" x="0" y="0"
                                         show="currentTarget.init(event)"
                                         />

                 

                this ofcourse does not work..

                what do i put in de "show=" to invoke the init of the component DomainView?

                • 5. Re: refresh component each time it becomes visible
                  rtalton Level 4

                  show="init(event)"

                  1 person found this helpful
                  • 6. Re: refresh component each time it becomes visible
                    jsd99 Level 3

                    ArjanDs wrote:

                     

                    group has the same capabilities of show/hide as in FB3

                    show

                    Event
                    Event Object Type: mx.events.FlexEvent
                    property FlexEvent.type = mx.events.FlexEvent.SHOW
                    Language Version: ActionScript 3.0
                    Product Version: Flex 3
                    Runtime Versions: Flash Player 9, AIR 1.1
                    so you can help anyway

                     

                     

                    ok so i have this in my Main.mxml

                     

                        <local:DomainView id="domainView"
                                             regentid="{regentid}"
                                             visible="{loggedIn}"
                                             includeIn="domainInfo" x="0" y="0"
                                             show="currentTarget.init(event)"
                                             />

                     

                    this ofcourse does not work..

                    what do i put in de "show=" to invoke the init of the component DomainView?

                     

                    You'll have to make DomainView's init method static and then you can call it in the show handler by doing DomainView.init(event)

                    1 person found this helpful
                    • 7. Re: refresh component each time it becomes visible
                      ArjanDs Level 1

                      ok thanks, but once i make the function static i am getting a whole bunch of other errors:

                       

                      init() in DomainView.mxml

                       

                                  protected static function init(event:FlexEvent):void
                                  {
                                      getRegentinfoByIDResult.token = regentinfoService.getRegentinfoByID(regentid as int);
                                  }

                       

                      gives:

                      Multiple markers at this line:
                      -1120: Access of undefined property getRegentinfoByIDResult. (is the callresponder)
                      -1120: Access of undefined property regentinfoService. (the service)
                      -1120: Access of undefined property regentid. (is passed from the main.mxml as bindable and declared in domainview as well... )

                       

                      the regentid can be solved to put static in front of that as well (does this interfere with the non-static one in the main.mxml?)

                       

                      [Bindable] static public var regentid:int;

                       

                      but the other two are declared in the fx:declarations of the DomainView

                          <fx:Declarations>
                              <s:CallResponder id="getRegentinfoByIDResult" result="getRegentinfoByIDResult_resultHandler(event)"/>
                              <regentinfoservice:RegentinfoService id="regentinfoService" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/>
                          </fx:Declarations>

                      • 8. Re: refresh component each time it becomes visible
                        ArjanDs Level 1

                        i defined the callresponders as static vars

                         

                                    [Bindable]
                                    public static var getRegentinfoByIDResult:CallResponder;
                                    [Bindable]
                                    public static var regentinfoService:RegentinfoService;

                         

                        but now the main.xml is complaining about the regentid (that one is now on static too in the domainview.

                        • 9. Re: refresh component each time it becomes visible
                          ArjanDs Level 1

                          isnt there a non-static way to solve this problem?


                          • 10. Re: refresh component each time it becomes visible
                            jsd99 Level 3

                            If you want to call instance methods (as opposed to class methods) you'll need to do it through event.currentTarget.

                             

                            So, make the init function a "static init" function, then call the "real" init from the static init, using event.currentTarget.

                            • 11. Re: refresh component each time it becomes visible
                              ArjanDs Level 1

                              ok, now i am completely lost ...

                               

                              so in DomainView i create a static function opposed to the "real" one

                               

                              lets say:

                                          protected function init(evenevent:FlexEvent):void
                                          {
                                              getRegentinfoByIDResult.token = regentinfoService.getRegentinfoByID(regentid as int);
                                          }
                                         
                                          public static function refresh(event:FlexEvent):void
                                          {
                                             
                                          }

                               

                               

                              in the main.mxml:

                                  <local:DomainView id="domainView"
                                                       regentid="{regentid}"
                                                       visible="{loggedIn}"
                                                       includeIn="domainInfo" x="0" y="0"
                                                       show="DomainView.refresh(event)"
                                                       />

                               

                              and now i need to put the event.currentTarget somwhere in the static refresh to point to init?

                              • 12. Re: refresh component each time it becomes visible
                                ArjanDs Level 1

                                i just put an Alert and trace in the "empty" static function refresh.

                                event though its called in the show, it is never triggered??

                                • 13. Re: refresh component each time it becomes visible
                                  jsd99 Level 3

                                  in the static refresh, do this:

                                   

                                  public static function refresh(event:Event):void  {

                                     event.currentTarget.init();
                                  }

                                  This will make sure that init is called on the particular instance that dispatched the event.  Also your init function currently takes an event parameter, you either need to make that optional or remove it entirely.

                                   

                                  i just put an Alert and trace in the "empty" static function refresh.

                                  event though its called in the show, it is never triggered??

                                   

                                  How are you making the component visible?

                                  • 14. Re: refresh component each time it becomes visible
                                    ArjanDs Level 1

                                    i was indeed only showing/hiding the State, and not the visible value as well.

                                     

                                    event.currentTarget.init();  needed "event" as argument as well.

                                     

                                    but its finally working!

                                     

                                    you saved my day!

                                     

                                    thnx in a milion

                                    • 15. Re: refresh component each time it becomes visible
                                      jsd99 Level 3

                                      You're welcome, please mark my answer as correct

                                      • 16. Re: refresh component each time it becomes visible
                                        ArjanDs Level 1

                                        i also found another way to refresh the component each time it becomes visible.

                                         

                                        instead of creating a static function in the component and invoking it by the show attribute in the main.mxml like

                                         

                                        DomainView:

                                                         public static function refresh(event:FlexEvent):void
                                                    {
                                                        event.currentTarget.init(event);
                                                    }

                                         

                                        Main.mxml:

                                            <local:DomainView id="domainViewer"
                                                                 regentid="{regentid}"
                                                                 visible="false"
                                                                 includeIn="domainInfo" x="0" y="0"
                                                                 show="DomainView.refresh(event)"
                                                                 />

                                         

                                        simply put the show attribute in the show of the DomainView.mxml header and calling the same init() (the non static method)

                                         

                                        <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
                                                 xmlns:s="library://ns.adobe.com/flex/spark"
                                                 xmlns:mx="library://ns.adobe.com/flex/mx"
                                                 width="1024" height="768"
                                                 initialize="init(event)"
                                                 show="init(event)"
                                                 xmlns:regentinfoservice="services.regentinfoservice.*">

                                         

                                        or is this a very wrong approach? (it does work..)

                                        • 17. Re: refresh component each time it becomes visible
                                          jsd99 Level 3

                                          That approach is just fine.  The only difference is that your new way will make the show handler run for every component of that type.  It wasn't clear from your original description if you needed it to happen with all components of that type or just some of them.

                                          • 18. Re: refresh component each time it becomes visible
                                            ArjanDs Level 1

                                            hmm well, it should only apply that that single component.

                                             

                                            but are you sure, the way it is setup now, the show is IN the component itself (not in the main)... so no other components refer to it.

                                             

                                            from what i understood from the flex in a week tuts is that the show event in a component is dispatched every time when the component goes from invisible to invisible.

                                             

                                            does not say it also affects other components

                                            • 19. Re: refresh component each time it becomes visible
                                              jsd99 Level 3

                                              I meant it would apply to all DomainView components.  If that's how you want it to work, then it's fine.  If there are DomainViews that you DON'T want to auto-refresh (for whatever reason) then you would have to remember to override their show handlers (or go back to the static version).  If you want them all to auto-refresh then your approach is fine.

                                              • 20. Re: refresh component each time it becomes visible
                                                ArjanDs Level 1

                                                ah ok, i understand, DomainView has no other components included, and the init only has the dataservice call.

                                                 

                                                but good to know the difference.

                                                 

                                                thnx again