2 Replies Latest reply on Jul 28, 2010 12:49 PM by Deh Bontempos

    Best Practice with States and lots of code lines

    Deh Bontempos



      This is my first application in flex.

      I'm ok with as3.


      Now, in as3 we were 'forced' to work mostly with external classes so hardly we have a unique code page with lots of lines.

      In flex, using States leads to build codes with lots of line IF we think on states as web site pages.


      I'm not sure it I understand it right. You mean: if an user visits a website built with 10 pages, but the users access only 2 pages, all that 8 remaining pages would have to be download to the swf the user loads?  (this is, considering the usage os states as pages)



      I'm building a system where the user logs to use it.

      2 states at now:  login page and home page.


      I access the db, and get the user and password with this event dispatched from the db.result (this works, however i found it too-old-style looping. Is there a better way, of course, which?)


      protected function usersService_resultHandler(event:ResultEvent):void
                      allUsers = event.result as ArrayCollection;
                      for (var i:uint;i<allUsers.length;i++){
                          if(allUsers[i].user == tx_user.text && allUsers[i].password == tx_password.text)
                              currentState = "home";
                              Alert.show("Fault", "Login");



      While I have start to build the "home" page/state, I realized that my code would dramatcally increase. Is it the best practice? Do I have to call another url after login (to open a Session - please, some Session tutorials in flex)? Or I keep doing all in states? I'm afraid my swf would grow bigger.



        • 1. Re: Best Practice with States and lots of code lines
          John Hall Level 4

          As far as your looping over the return from the database, consider simply making a database call that returns the one record where username and password is matched. If the return is null, one of the two is wrong and you can let the user know.


          In the loop. when you use one, and if you're using arrayCollections instead of Arrays, use getItemAt instead of the Array syntax of []


          so allUsers.getItemAt(i).password


          If you prefer to act on the array (which has some advantages for speed with large datasets in some cases), you would use your syntax on allUsers.source.


          As far as states a filesize, at least you're asking the right questions. I tend to create different views (i.e., different components to use) rather than through everything into one view with a bunch of different states. The reason is as you mention, why load stuff you don't need yet. If you read a bit about lazy loading and the creation cycle of Flex components, you will see that, as you surmise, if you keep things separate and use creationPolicy smartly, the amount that has to load immediately is relatively small. So not knowing what type of app you're working on, let's say you have a tab navigator which houses 10 tabs of potential content. If the content of each of those tabs is actually a different form, you might create the forms separately and then put those custom made components into the containers of the navigatorcontent. As long as you don't turn on creationPolicy = "all" for the tab navigator, the app should only load what it needs as the user navigates.


          If you have a huge app ahead of you, I'd look into using Modules as they literally don't load until you need them (though also Google about how to unload modules and the things that can keep them in memory).

          1 person found this helpful
          • 2. Re: Best Practice with States and lots of code lines
            Deh Bontempos Level 1



            The problem is that I'm not used to PHP, and I have generated the code to deal with the server automatically via Flex.

            However I could add a new function, and I could guess how to catch values in the db to compare.


            its a frankenstein function, but i'm afraid it also works. By now, there is no way to know whether user mistyped password or username.

            public function getUserVerification($user, $pass) {
                    $stmt = mysqli_prepare($this->connection, "SELECT user, password FROM $this->tablename where user=? AND password=?");
                    mysqli_stmt_bind_param($stmt, 'ss', $user, $pass);       
                    mysqli_stmt_bind_result($stmt, $row->user, $row->password);
                    if(mysqli_stmt_fetch($stmt)) {
                      return 1;
                    } else {
                      return 0;


            Also I had to update the _Super_UsersService.as  Class flex had generated before when I first created a php code to deal with db.

            Finaly, I had to assign return and input types for the new function I've created.


            Amazing... it works.


            Now, when pressing the submit button on the login, flex sends user and password so php compare them instead looping it in a Array.

            Also, I have made all this code inside a "loginView" component. So my main app is clean again.


            I guess I understand the idea of using components and reusing them as many as possible. I just have to get used to how to access a component value from outside and vice-versa.


            Now, the creationPolicy is something I would look for. This might be interesting.

            Thanks a lot.