4 Replies Latest reply on Apr 25, 2010 11:59 PM by PieterBosman

    'Basic' external actionscript file/class usage

    PieterBosman

      Hey All,

       

      I'm a bit baffled by something that should, in my humble opinion, be a piece of cake...

       

      The setup is really easy. A simple application with a single textinput, a button and a text component.

      When the button is clicked a function is called that sets the text from the inputfield to the text component.

       

      code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:Script>
              <![CDATA[
                  public function click(inputText:String):void {
                      outPutTxt.text = inputText;
                  }
              ]]>
          </mx:Script>
          <mx:TextInput x="10" y="10" id="inputTxt"/>
          <mx:Button x="178" y="10" label="Button" id="Btn" click="{click(inputTxt.text)}"/>
          <mx:Text x="251" y="12" text="Text" id="outPutTxt"/>
      </mx:Application>

       

      As I said, really easy.

       

      What I want to do is put the function 'click' into an external actionscript file or class. And still be able to, from within the external file, modify the outPutTxt stuff.

       

      I'm aware that I could create a public bindable variable inside of the external file and then bind the outPutTxt to that variable, but I would really like to be able to 'simply' force the value...

       

      Is that possible? If yes: how plix

       

      If it's not possible, I'd like to know as well! And in that case, how would I then re-use the same instance of a class/file in multiple seperate components? Say I got a 'login' thingie where a user logs in (data stored externally in a seperate actionscript file/class) and I want to re-use that information on other pages... Or is this done automatically?

       

      Any and all comments/answers are higly appreciated!

       

      Pieter

        • 1. Re: 'Basic' external actionscript file/class usage
          levancho Level 3

          I am not entirely clear on what exactly you are trying to achive but  here is what I understood :

          there many ways to decouple things like that, but I will write two of them

           

          1) if you put function in external as file, not a class, you just simply includethat as file in that mxml and thats it nothign extra is needed

           

          2) if you have external class

          for example myclass  you can do following:

           

          myclass {

           

            public function  click(inputText:String):void {
                          this["
          outPutTxt"].text =  inputText;
          }

           

          }

           

          then in your mxml:

           

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

                                   private var myClassInstance:MyClass = new MyClass();// or isntantiate whatever way you want, in creationCompete etc ,..

                   ]]>
              </mx:Script>
              <mx:TextInput x="10" y="10"  id="inputTxt"/>
              <mx:Button x="178" y="10" label="Button"  id="Btn" click="{myClassInstance
          .click.call(this,[inputTxt.text])}"/>
              <mx:Text x="251"  y="12" text="Text" id="outPutTxt"/>
          </mx:Application>

           

          have not tested , but should work.

           

          on the sidenote: i am not sure why would you need to do that, if you clarify your question might be usefull.

          • 2. Re: 'Basic' external actionscript file/class usage
            PieterBosman Level 1

            Hey Levancho,

             

            thanks for your answer!! Much appreciated.

            However, the code you proposed threw an error...

            Errormessage:

            ReferenceError: Error #1069: Property outPutTxt not found on controls.Control and there is no default value.
                at controls::Control/btnClick()[C:\...\FlexTest_03\src\controls\Control.as:13]
                at FlexTest_03/__Btn_click()[C:\...\FlexTest_03\src\FlexTest_03.mxml:11]

             

            So I tried something else where I bind the text in my OutPutTxt to a variable inside of the external class file.

             

            Code:

            Class:

            package controls
            {
                public class Control
                {
                    [Bindable]
                    public var outPutText:String = "";
                   
                    public function Control()
                    {
                    }
                    public function btnClick(inputText:String):void {
                        this.outPutText =  inputText;
                    }

             

                }
            }

            Mxml:

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
                <mx:Script>
                    <![CDATA[
                        import controls.Control;
                        [Bindable]
                        private var control:Control = new Control();
                    ]]>
                </mx:Script>
                <mx:TextInput x="10" y="10" id="inputTxt"/>
                <mx:Button x="178" y="10" label="Button" id="Btn" click="{control.btnClick(inputTxt.text)}"/>
                <mx:Text x="251" y="12" id="outPutTxt" text="{control.outPutText}"/>
            </mx:Application>

             

            And this basically did what I wanted to do... so I just need to do some major binding between my application and the classes I'll be using for my business logic.

             

            Now for something else entirely...

            Is there a way for me to pass the instance of my class between flex components?

             

            Pieter

            • 3. Re: 'Basic' external actionscript file/class usage
              levancho Level 3

              of course you would get this error, since you did not use .call like in my example.

               

              control.btnClick.call

               

              vs

               

              control.btnClick

              1 person found this helpful
              • 4. Re: 'Basic' external actionscript file/class usage
                PieterBosman Level 1

                Ah yes... *feels silly*

                Thanks for your help levancho

                Now it does indeed what I want it to do !