14 Replies Latest reply on Jun 16, 2009 8:57 PM by GordonSmith

    Setting a TextInput text field from an actionscript file

    sam luke

      hi,

        I am being a bit slow again : (  From my actionscript class file I load a text file.  I then want to set the text in various textinput components.  But allow the user to be able to update these fields.  But only update these when the user hits enter.   Also I want to allow the appropriate class variables be updated.  From my actionscript I initally thought I could call the text property and set it.  For example:

       

      mxml       
                <mx:TextInput id="myText" ...

       

      actionscript

              public function Init():void {
                  myText.text = "test";
              }

       

      But it doesnt compile!!!!  Because it doesnt know what myText is?

       

      I then made the class variables bindable.  This was fine but I dont think it will give me exactly what I want and I think I am using it for the wrong reasons...

       

      So my question(s) are:

       

      1. How do I successfully set the TextInput text property from within my class?

      2. When I make a class variable bindable will it change the variable instantaneously. ie if user types number '123'.  Does the variable get set to '1', then '12' and then '123?

       

      Thanks for your time,

        Paul.

       


       


        • 1. Re: Setting a TextInput text field from an actionscript file
          BennyBeta Level 4

          1. The code you show should work.  Are you sure there's no typos?  And is the AS and MXML you show in the same file?

          2. Binding doesn't truly happen instantaneously, rather asyncrhonously.  Still, yes, the binding will respond to each character: '1', then '12' and then '123'.

           

          Let me know if that helps or if you have more questions...

           

          Ben Edwards

          • 2. Re: Setting a TextInput text field from an actionscript file
            sam luke Level 1

            Thanks for your response Ben.  The AS and MXML are in different files though!  Do I need to do something extra if they in differnet files?  The MXML is currently source 'ing a .as file that instantiations the class file.

             

            Hope that all makes sense.

             

            Thanks,

            Paul.

            • 3. Re: Setting a TextInput text field from an actionscript file
              BennyBeta Level 4

              Because they are in different files, that's why you can't say just myText.text.  Keep them in the same file for now (until you get things working).  After you get it working then worry about splitting up the files (if you still think it's necessary).

              • 4. Re: Setting a TextInput text field from an actionscript file
                sam luke Level 1

                Hi Ben,

                     Thanks for keeping things simple.  Once I put it into the same file I could access it.  Although I think that I need to understand the bigger picture of having multiple packages and classes, then how to access the components in the mxml file.  I have listed the kind of file structure I have and what I want to achieve.

                 

                          main.mxml                         functions.as                         myManager.as

                 

                          include "functions.as"-------->

                                                                   import myManager------------->

                          id=myTextInput" <--------------------------------------------------------myTextInput.text ="test";

                 

                Hope you can help,

                  Paul.

                • 5. Re: Setting a TextInput text field from an actionscript file
                  Michael Borbor Level 4

                  Probably you need to know the difference between importing and including an

                  AS3 class, or file.

                  http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_4.html

                  • 6. Re: Setting a TextInput text field from an actionscript file
                    GordonSmith Level 4

                    I'm afraid that I can't figure out what your diagram is trying to show. But here are a few pointers that might help you:

                     

                    1. You should generally not be using the "include" statement.

                     

                    2. Writing a Flex app involves writing one or more classes, either in MXML or in AS. Each MXML file augenerates an AS class behind the scenes.

                     

                    3. Classes use other classes by importing them, not including them.

                     

                    4. When you write a tag like <mx:TextInput id="myText"> in MyApp.mxml, a public var gets generated in the MyApp class, so any code in that same class can refer to 'myText' directly. But code in other classes cannot refer to it. The other class has to somehow obtain a reference to the application object. Similar rules apply if the TextInput is in a component rather than in an application... you can only refer directly to 'myText' from the class where the tag was declared.

                     

                    Gordon Smith

                    Adobe Flex SDK Team

                    • 7. Re: Setting a TextInput text field from an actionscript file
                      sam luke Level 1

                      Thanks for your response everyone.  Although I am not completely sure if I can achieve what I thought I could now.

                       

                      Are you saying that I cant access say a textInput from another external class file?

                       

                      I have tested that I can access the textinput.text property from a included .as file.  Thats works : )  My problem lies with referencing the textInput.text property from within another class file.  Surely you must be able to do this!!!

                      • 8. Re: Setting a TextInput text field from an actionscript file
                        sam luke Level 1

                        Hi everybody - who is reading,

                          I have spent several more hours looking at this with no joy : (  This to me is fundamental behaviour of how actionscript links to flex.  I have created a test project to describe what I am trying to achieve.  You can see have listed the mxml and as files below.  When I try and compile the actionscript file I get the error Access of undefined property test - highlighted orange.  CAN SOMEONE PLEASE HELP ME OUT ......

                         

                        mxml_actionscript_linkage_test.mxml

                         

                         

                         

                         

                         

                         

                         

                        <?xml version="1.0" encoding="utf-8"?>

                        <mx:Application

                         

                        xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >

                         

                         

                         

                        <!-- DESIGNED TO TEST LINKAGE BETWEEN MXML FILE AND ACTIONSCIPT FILE -->

                         

                         

                         

                             <mx:TextInput id="test" text="Start" />

                         

                         

                         

                             <mx:Script>

                                  <![CDATA[

                         

                         

                                       import myLinkage;

                         

                         

                         

                                       public var linkage:myLinkage = new myLinkage;

                                  ]]>

                         

                             </mx:Script>

                         

                        </mx:Application>

                         

                        myLinkage.as

                         

                         

                         

                         

                         

                        package

                        {

                         

                         

                             public class myLinkage {

                         

                         

                                  public function

                                       myLinkage() {

                                       }

                         

                         

                                       public function changeText():void {

                                            test.text =

                        "starting";

                                       }

                             }

                        }

                        • 9. Re: Setting a TextInput text field from an actionscript file
                          Michael Borbor Level 4

                          if you want to access controls and properties from the AS3 file, you must include the file, so this file becomes part of the parent app or class. If you import it you could use Application.application, or better change the approach to what you want to do and accomplish.

                          • 10. Re: Setting a TextInput text field from an actionscript file
                            sam luke Level 1

                            Thanks Michael for your answer.  I have just tried the Application.application again and managed to get it work.  I have tried this before but couldnt for some reason!!!!  Anyway thanks for your answer.

                             

                            Why do you say change your approach?  Is using Application.application not good to use?

                            • 11. Re: Setting a TextInput text field from an actionscript file
                              Michael Borbor Level 4

                              Most of the time when you create an AS class, you do it so, you can reuse this class throughout your app, or multiple projects so using Application.application is hardcoding something in your class, so then your class would lose the loose-coupling. I really haven't read this whole thread, so forgive if I make a wrong assumption but if you want to know the text or set the text from a textinput or any other control within your AS class, you could create a function that expects a textinput as a parameter and then call this function from your mxml application and set the textarea that you want your AS3 class to interact with, or if you want to set the value then you could create a function that just returns a string.

                               

                              Although in this case when you just want to separate the mxml file from the as file you don't need a class, you can just create a bunch of functions in the as file, then using the mx:Script source="myasfunctions", include this file in your mxml file and it'll compile fine. Remember that just because a file is in the same path that another file, that doesn't mean that they're related, or that one file can access the other properties,there must be some sort of relation in there.

                              • 12. Re: Setting a TextInput text field from an actionscript file
                                GordonSmith Level 4

                                Your problem has nothing to do with how MXML links to AS. As I said before, when you write an MXML application or component, it is just convenient shorthand for writing an AS class. Your problem seems to be in not understanding which object a property or method belongs to, or how one AS object accesses properties and methods on another AS object. Both of these concepts are fundamental to object-oriented programming, so let me try to clarify them.

                                 

                                In object-oriented programming, when you declare classes like

                                 

                                public class C1

                                {

                                    public var p1;

                                }

                                 

                                public class C2

                                {

                                    public var p2;

                                }

                                 

                                property p1 belongs only to instances of C1 and property p2 belongs only to instances of C2. In fact, the memory used to store p1 is inside each instance of C1, and the memory used to store p2 is inside each instance of C2.

                                 

                                Since a property belongs to a particular object, the notation o.foo is used to mean "the value of property foo inside object o". Suppose one object c1:C1 creates another object c2:C2. Now c1 can simply use the notation 'p1' to access its own property p1. (You can write it as 'this.p1' if you want, but you don't have to.) But to access C2's property p2, it must use the notation 'c2.p2', and must therefore have the reference c2 to the instance of C2.

                                 

                                In your case, you've defined an MXML application, which I've taken the liberty of renaming MyApp.mxml:

                                 

                                <Application>

                                    <TextInput id="test"/>

                                    <Script>

                                        public var linkage:MyLinkage = new MyLinkage();

                                </Application>

                                 

                                This is MXML-ese for writing the AS class

                                 

                                public class MyApp extends Application

                                {

                                    public var test:TextInput = new TextInput();

                                    public var linkage:MyLinkage = new MyLinkage();

                                }

                                 

                                The 'linkage' object that you create does not have a 'test' property... this is a property of instances of MyApp. So the question is... how can the 'linkage' object obtain a reference to the MyApp instance that created it?

                                 

                                There are at least two ways. The first is that you pass it one -- for example, at construction time -- by doing something like declaring

                                 

                                public class MyLinkage

                                {

                                    public function MyLinkage(myApp:MyApp)

                                    {

                                        this.myApp = myApp;

                                    }

                                 

                                    private var myApp:MyApp;

                                 

                                    public function changeText():void

                                    {

                                        myApp.test.text = "starting";

                                    }

                                }

                                 

                                You would then create the instance of MyLinkage inside MyApp by doing 'new MyLinkage(this)', since 'this' will be the instance of MyApp that is doing the creating.

                                 

                                The second way is more straightforward, since you're trying to get to something that happens to live on the application instance. Since the application object is unique and it's common to want to access it, we've made that easy in the Flex framework: any object can access the application instance as Application.application. So you can just do

                                 

                                public function changeText():void

                                {

                                    MyApp(Application.application).test.text = "starting";

                                }

                                 

                                What you CAN'T do is expect for code in a random class like MyLinkage to access a property 'test' in a different class MyApp without having a reference to the instance of MyApp. After all, there might be a dozen different classes with a 'test' property -- and even if there is only one class with a 'test' property, there might be a dozen different instances of it -- so you have to say which instance to look up 'test' on.

                                 

                                Gordon Smith

                                Adobe Flex SDK Team

                                • 13. Re: Setting a TextInput text field from an actionscript file
                                  sam luke Level 1

                                  Thank you for everyone who responded.  I understand exactly what you are saying.  I appreciate you spending the time to explain : )

                                  • 14. Re: Setting a TextInput text field from an actionscript file
                                    GordonSmith Level 4

                                    Great! In that case, can you please mark this as Answered?

                                     

                                    Gordon Smith

                                    Adobe Flex SDK Team