8 Replies Latest reply on Aug 30, 2008 8:05 AM by cj_c

    Action Script Question.....

    Gene Godsey Level 1
      I have below code that iterates through XML data from record to record.
      It is like going to the next record hitting the nexr record and going to the previous record hitting previous.
      I have the previous button not appear when it is loaded but what I need is when the final record is selected I want the next button to disapear and have visable as false. This would prevent the end user running into a runtime error of no valuse selected because therfe is no record to select.

      Please review code and make suggestions.
      Thanks in advance.
      ----------------------
        • 1. Re: Action Script Question.....
          Level 7

          "Gene Godsey" <webforumsuser@macromedia.com> wrote in message
          news:g991i4$rd4$1@forums.macromedia.com...
          >I have below code that iterates through XML data from record to record.
          > It is like going to the next record hitting the nexr record and going to
          > the
          > previous record hitting previous.
          > I have the previous button not appear when it is loaded but what I need is
          > when the final record is selected I want the next button to disapear and
          > have
          > visable as false. This would prevent the end user running into a runtime
          > error
          > of no valuse selected because therfe is no record to select.
          >
          > Please review code and make suggestions.
          > Thanks in advance.
          > ----------------------
          >
          >
          > private function clickHandler(event:MouseEvent):void{
          > var btnName:String = event.target.id;
          > if(btnName == "btnPrev"){
          > imgCounter --;
          > } else if (btnName == "btnNext"){
          > imgCounter ++;
          > }
          > showImage();
          > if(imgCounter == 0){
          > showButtons(false, true);
          > } else if (imgCounter == cboGalleries.selectedItem.photos.length() -1) {
          > showButtons(true, false);
          > } else {
          > showButtons(true, true);
          > }
          > }

          What's in showButtons?


          • 2. Re: Action Script Question.....
            Gene Godsey Level 1
            Those are the next or previous buttons
            • 3. Re: Action Script Question.....
              Level 7

              "Gene Godsey" <webforumsuser@macromedia.com> wrote in message
              news:g9961b$2oi$1@forums.macromedia.com...
              > Those are the next or previous buttons

              showButtons() looks like it ought to be a function that's expecting two
              arguments. If you _don't_ have a showButtons function, I'm amazed you're
              not getting any compiler or runtime error.


              • 4. Re: Action Script Question.....
                yigitt Level 1
                are you a flash coder or need to code this thing in pure as3?
                If I were you, I would use data binding to control button visibility(if you use pure as3, there is no advantage, but when u use mxml, you'll have more clear code). So sth like this:

                //as3
                [Bindable]
                public var imgCounter:int=0;

                function prevImage() {
                imgCounter--;
                showImage();
                }


                function nextImage() {
                imgCounter++;
                showImage();
                }
                //mxml:
                <mx:Button id="btnPrev" click="{prevImage()}" enabled="{imgCounter > 0}" />
                <mx:Button id="btnPrev" click="{nextImage()}"
                enabled="{cboGalleries.selectedItem.photos.length() - imgCounter > 0}" />
                • 5. Re: Action Script Question.....
                  yigitt Level 1
                  btw, I just wanted to keep concurrent with your code.
                  If I were you, I would implement showImage as a funtion which gets the image id. bcuz having two function prevImage-nextImage just decreasing an int and calling another function is not much logical. (but if you do more things, it will be good to utilize)
                  • 6. Re: Action Script Question.....
                    Gene Godsey Level 1
                    PURE AS# please.

                    This is for a bigger application where I am maintaining the code in the script blok not inline coding using MXML.
                    Thanks,
                    Gene
                    • 7. Re: Action Script Question.....
                      Ratsnackbar Level 2
                      Sorry I realize you want this in AS3. I just happened to have an example in MXML I thought could be of service. Doing the same thing in AS3 is very easy. But I will leave it to you to figure out how.
                      Just remember that you can use bindings in AS3 also. (Hint!)

                      NOTE: I also used strings here but the same technique can be used with images, lists or anything you can get to with an index really.

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical">

                      <mx:Script>
                      <![CDATA[
                      import mx.collections.ArrayCollection;

                      private var imageArray:Array = ["Image1","Image2","Image3","Image4","Image5"];

                      [Bindable] private var displayList:ArrayCollection = new ArrayCollection(imageArray);

                      [Bindable] private var displayCounter:uint = 0;

                      ]]>
                      </mx:Script>
                      <mx:Text id="display" text="{displayList.getItemAt(displayCounter)}" />
                      <mx:HBox id="btnBox">
                      <mx:Button id="previousButton" visible="{Boolean(displayCounter)}" label="Previous" click="displayCounter--" />
                      <mx:Button id="nextButton" visible="{Boolean((displayList.length - displayCounter)-1)}" label="Next" click="displayCounter++" />
                      </mx:HBox>
                      </mx:Application>
                      • 8. Re: Action Script Question.....
                        cj_c
                        Hi Gene,

                        I am doing a similar thing by paging through a larger resultset on the server with linkbuttons. Each previous or next click I need to make a service call to the server for the appropriate page of data. I use two AS3 functions, one for navigation control, the other for button state control.

                        The navList arguments sibling and event are passed from the click event on each button. The sibling argument is appropriately set to the other button when needed. So when previous is clicked sibling is the next linkbutton and when next is clicked sibling is the previous linkbutton. My get_data() does a httpservice send to get the page data. The pgIndex tells the service which page I want and pgTotal is returned from the service.

                        private function navList(sibling:LinkButton, event:Event):void
                        {
                        /* control page index values when link buttons previous and next are clicked */
                        switch(event.target.id)
                        {
                        case "btnNext":
                        pgIndex++; //pgIndex is used for the service
                        pgIndex = (pgIndex > pgTotal)?pgTotal:pgIndex;
                        break;
                        case "btnPrevious":
                        pgIndex--;
                        pgIndex = (pgIndex < 1)?1:pgIndex;
                        break;
                        }
                        var clicked:LinkButton = event.target as LinkButton;
                        btnState(sibling, clicked);
                        get_data();
                        }

                        private function btnState(sibling:LinkButton, clicked:LinkButton):void
                        {
                        /* manage enabling/disabling link buttons */
                        switch(clicked.id)
                        {
                        case "btnNext":
                        clicked.enabled = (pgIndex==pgTotal)?false:true;
                        sibling.enabled = (pgIndex==1)?false:true;
                        break;
                        case "btnPrevious":
                        clicked.enabled = (pgIndex==1)?false:true;
                        sibling.enabled = (pgIndex==pgTotal)?false:true;;
                        break;
                        }
                        }

                        I like how Ratsnackbar does his enabling and disabling within MXML and am looking at how to implement in my code. Here is what I have for the MXML piece...

                        <mx:LinkButton label="&lt;&lt; Previous" textAlign="right" id="btnPrevious"
                        click="navList(btnNext,event)" enabled="false"/>

                        <mx:Label text="|" textAlign="center" width="15"/>

                        <mx:LinkButton label="Next &gt;&gt;" width="67" textAlign="left" id="btnNext"
                        click="navList(btnPrevious,event)" enabled="false"/>

                        The initial state is disabled so I call the btnState function in my http service result handler.

                        Hope this helps,
                        CJ