tags:

views:

62

answers:

7

Hi there,

i allready searched abit around here and found the solution to call a fx:script function defined in a mxml from a AS class.

// MXML
<fx:Script>
 <![CDATA[
            public static function write(text:String):void{
   myTextfield.append(text)
  };
 ]]>
</fx:Script>


// ActionScript Class
import MXMLPackage
private function callWrite():void{
 myMXML.write("My text");
};

The thing that had to be done for this to work was to set the function static, otherwise i'm not able to call the function from the AS. So now the next Problem: now that the function is static, i can't access the myTextfield component anymore.

Does anybody know why and / or a workaround for this problem?

A: 

well, i basicly created a textfield (here called myTextfield) component within my mxml and added a write function to append text to this textfield.

now i want to call this function from my actionscript. therefor i imported the mxml package and simply called the function. this works only if the write function is set to static (dont know why).

but when set to static the function cant call the myTextfield component anymore - which also dosn't make much sense to me.

masi
Flextras is right on this one, you need to understand the difference between the declaration of a class and an instance of a class. Using the "static" keyword only applies to a declaration of a class, not an instance of that class. It makes perfect sense to me that you can't access myTextfield from a static method. This is because myTextfield will never be created until your mxml class has been instantiated. I think if you look closer at Flextras answer you will see that you probably don't need to use "static" and can just make your function public.
Wade Mueller
you should read what we've written above in more detail.
RJ Owen
+2  A: 

Static functions are called on a class. Non-static functions are called on an instance of the class.

As far as I know, there is no way to create functions in MXML. You can create a function in an MXML component, though. And the code you display does that. HEre is what may be an expanded version of your component without the static function

<mx:VBox>
    <fx:Script>
     <![CDATA[
                public function write(text:String):void{
       myTextfield.append(text)
      };
     ]]>
    </fx:Script>
<mx:textInput id="myTextField" />
    </mx:VBox>

In some other class you can create an instance of the component:

<myNameSpace:myVBox id="myVBox" />

And in that component, which creates an instance of myNameSpace you can access:

myVBox.write('some value');

But, I'm not sure if this helps.

www.Flextras.com
A: 

Hmm, yes - but you're accessing the function from within another mxml.

i'm trying to access it from an actionscript class.

to be more clear:

MainApp.mxml

<fx:Declarations>
    <local:myASScript id="aSScript"/>
</fx:Declarations>

<fx:Script>
    <![CDATA[
        public static function write(text:String):void{
        myTextArea.appendText(text+"\n");
        }
    ]]>
</fx:Script>

<s:TextArea width="400" height="70" id="myTextArea" x="395" y="7"/> 

Second File

myASScript.as

package logic{
    import main.MainApp;

public class TestClass{
     public function TestClass():void{
          mainApp.write("Here it comes...");
     };
}}

The confusing part about this: when set to static (as here), the write function is accessable from the ASScript, otherwise not. BUT when set to static, the write function cant access the myTextArea anymore. And thats my problem.

masi
A: 

Wade , i know that. But however, when not set to static, i can't access the function from my AS Class. See image.

http://img340.imageshack.us/img340/1356/blablubu.jpg

the thing with the instance is: i allready got an visible instance of mytextarea, created by the mainApp.mxml, and i want to write into exacly that instance from an AS script. i dont want to create a new instance.

i was thinking that the main.mxml is instantiated automaticly when i run it (since its my main application from which i start) - so there should be no need to instantiate it again. or do i get that wrong?

masi
A: 

so - i probably just don't get it, could anyone maybe explain it once again?

i understand that the mxml tags are nothing else than classes and therefore have to be instantiated. but since those mxml tags (includeing the fx:script tag) are inside my main.mxml and since that is the application which is run at startup those classes are allready instantiated. i dont want to instantiated them again, also i dont want to build something as a singleton pattern - since for such simple write functions i see no sense in that. i just want to use the write method in the mxml from an AS class, and i dont want to create another instance of the - allready instantiated - mxml classes.

masi
A: 

It looks like your problem is just that you didn't create an instance of your myMXML object before you tried to call the class - that's why the method had to be static.

You have this:

// ActionScript Class
import MXMLPackage
private function callWrite():void{
 myMXML.write("My text");
};

Should be this:

// ActionScript Class
import MXMLPackage
private function callWrite():void{
 var _myMXML : myMXML = new myMXML();
 _myMXML.write("My text");
};

That will work without making the method static. Your problem is that myMXML is the name of a CLASS - not an INSTANCE of the class. You can only call STATIC methods on CLASSES - you can call non-static methods on INSTANCES of the class. To make this more easy to see common convention requires you capitalize the name of classes and not capitalize the first letter for instance variables. This would require you re-name your class as MyMXML, and the code becomes more clearly something like this:

// ActionScript Class
import MXMLPackage
private function callWrite():void{
 var _myMXML : MyMXML = new MyMXML();
 _myMXML.write("My text");
};

Notice that now even StackOverflow's code formatter realizes that MyMXML is a class name and not an instance and so colors it differently. :)

Here's the short version of what's going on (which others have explained above as well to varying degrees): your application knows about both the class itself and every instance of that class. Every instance gets it's own memory for member variables and things like that and lives a totally separate life from the others. The global class itself can be given static variables that all instances share and static methods that can be called without having to instantiate the class. Static methods are useful for things like utility classes, where you want the class to perform some action and return the result but have no need to keep an instance of the class hanging around in memory. In your example and in every case where you're building a UI related class, you want to create an instance of that method - you should really never try to use an MXML class statically (at least I can't think of a good reason why you would.)

This is the same in every modern OO language such as Java or C#. For a more complete reading on the subject check out some of these discussions on how it works in those languages:

http://leepoint.net/notes-java/flow/methods/50static-methods.html http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/java/javaOO/classvars.html http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx

Hope this helps!

RJ Owen
A: 

In case anyone has the same problem, i found what i was looking for:

mx.core.FlexGlobals.topLevelApplication.YOUR_FUNCTION

is the syntax to access public functions within the main.mxml.

Dunno why flex has a own function to do this and dosn't seem to be capable to access the function with the main.YOUR_FUNCTION syntax except the function is static. weired.

masi