3 Replies Latest reply on Jul 1, 2008 10:22 AM by francois-yanick

    Calling a function only when the state is active

    francois-yanick Level 1
      I am turning around to search the right event to call a function only when the state is active. In fact, I have created a state to show the "details" of a product clicked and once the system change to that state - than I have to run a function.

      Here is the context:

      Let's say that you are on the home page where you need to click a product to see is details. By clicking the product, the state to view the product's details will be activated and a variable inside this new state will receive all data (from a product class). But I discovered that Flex run all states at the beginning and we have to be carefull how to declare our function in order to make sure we are not running a null reference to a variable/object. Since the value is pass to the state only when the click event happen, the globalization process of running all states at the beginning will not have any value yet (concerning the specific product to process in the productDetails's state). So I cannot use creationComplete to run a simple function like setSpecs() where all values (such as width, height, circonference, diameter, price, etc...) will be instiate specific labels.

      What is the event to use to call an internal function once we are in the state???? Just using the creationComplet and delcare a if to verify it the currentState = the one I need to work on is not enought. I tried "activate", "show", "enterStage" and many others but without any success. I tried to search on the Internet but it seems that this problem is not enough popular to generate sufficient result.

      But I know it is possible otherwise it will be not logic and since Flex work fine with event, I just have to find the right event. This is somethign basic I am sure but when you don't know, it seems hard to find!!! ;-))
        • 1. Re: Calling a function only when the state is active
          francois-yanick Level 1
          I have to add the fact that the state call a separate file as a component that contain all the elements to display in that state. So all functions concerning that state will not be part of the main state/file.

          It might be confusing and if you need more details about this, feel free to let me know since putting code about this issue might be much more confusing compare to simplicity of knowing which event to use.
          • 2. Re: Calling a function only when the state is active
            Gregory Lafrance Level 6
            I would not use states, I would use a ViewStack for this. States are usually used for smaller components such as login screens, etc. ViewStack is a good way to provide navigation in your app to various component pages.
            • 3. Calling a function only when the state is active
              francois-yanick Level 1
              Thank you Greg!

              This is a better method, I agree. So I will change my appliccation in order to use viewStack instead of State. But the problem persist whatever if we are using state of viewStack in order to run an event only when the state/viewStack is displayed to the screen.

              I found an event that works fine for me: updateComplete.

              I first found this before reading your information concerning viewStack so it was working fine for state method. Than, I decided to give a try to viewStack and made the appropriate change in the code to reflect a navigation by viewStack and make sure that the updateComplete was not inside my component (stateProduct.mxml). The system was not able to display the specs as the function setSpecs() was not instantiate when displayed. Than I added the updateComplete="{setSpecs()}" and everything was working fine! So I conclude that whatever if you are using state of viewStack, the event updateComplete can serve to run function(s) only when the component is displayed on screen.

              Now I have to make my effect of transition between state working with viewStack!!! ;-))

              Thank you Greg for this useful hint!