5 Replies Latest reply on Sep 7, 2010 4:31 AM by Arun Ganesh

    Move code from mxml to actionscript

    Arun Ganesh Level 3

      Hello Everyone,

          I have one question. We are going to create an application using MVC pattern. I just wondering how should we keep scripts in separate class file instead of written mxml script tag.

           Class have to manage the events as well (including component event which are used in mxml form).

       

      Thank You.

        • 1. Re: Move code from mxml to actionscript
          betheflexcoder Level 2

          You can just use include actionscript file in mxml. specify something like the below code in your script tag of your mxml.

           

          In flex 4,

           

          <fx:Script source="includes/IncludedFile.as"/>

          In Flex 3,

           

           

          include "file_name";

          • 2. Re: Move code from mxml to actionscript
            Arun Ganesh Level 3

            Thanks.

             

            Can you please let me know, how should we handle the event on that class.

             

            For Example,

             

            <mx:Script>
                    <![CDATA[
                        import mx.controls.Alert;
                         [Bindable]
                        private var count:Number=0;

             

                        //this function should be called at application creation complete

                        private function init():void{            
                            //some action
                        }


                        private function changeTheText():void{
                           count++;
                        }


                    ]]>
                </mx:Script>
                <mx:VBox>
                    <mx:TextArea text="{count}"/>
                    <mx:Button label="submit" click="{changeTheText()}"/>
                </mx:VBox>

            • 3. Re: Move code from mxml to actionscript
              betheflexcoder Level 2

              once you include the actionscript file, it is

              actually in your mxml itself.

              I mean, think of just removing you actionscript code in <ms:script> tag from you mxml putting it in a separate file. just have your handler in that file and call it as usual.

               

              call like this:

               

                  <mx:Button label="submit" click="changeTheText()"/>

               

               

              and your AS file is

               

                import mx.controls.Alert;
                           [Bindable]
                          private var count:Number=0;

               

                          //this function should be called at application creation complete

                          private function init():void{            
                              //some action
                          }


                          private function changeTheText():void{
                             count++;
                          }

              • 5. Re: Move code from mxml to actionscript
                Arun Ganesh Level 3

                Thanks Dude. It works