actionscript-3flex4.5mxml

How to include an actionscript function from an external file in flex4/MXML/Spark?


It turns out that’s it’s impossible to declare a class inside a embedded <fx:Script><![CDATA[ so it turns I need to put and include the actionscript code inside an external Sourcefile. The error is commented out

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="1955" minHeight="1600">
    <fx:Script source="URLRequest.as" />
    <s:layout>
        <s:BasicLayout />
    </s:layout>
    <s:Panel x="0" y="0" width="955" height="600" title="Bypass">
        <s:layout>
            <s:BasicLayout />
        </s:layout>
        <s:Label x="1" y="1" text="Please enter the ɢɪᴛ repository ʜᴛᴛᴘ ᴜʀʟ :"/>
        <s:TextInput x="224" y="1" width="726" id="txtName" text="http://ytrezq.sdfeu.org/flashredirect/?http"/>
        <s:Button x="1" y="12" label="ɢɪᴛ push !" click="send()"/> <!-- Undefined Method method error -->
    </s:Panel>
    <fx:Declarations>
    </fx:Declarations>
</s:Application>

and in URLRequest.as :

final public class MyClass {
    // some stuff
}
public function send():void {
    var request:Myclass=new Myclass(txtName.text);
    // Some stuff with 
}

So the question is simple but I couldn’t found the answer anywhere. At least not with for mxml with Spark.
send() doesn’t need to be in a class and as you can see is outside a class. But it needs to use a custom class.

So how can I call send() from URLRequest.as ?


Solution

  • Now that I finally understand what you want to do, I've another idea - which is a little more complicated.

    Create a file called Dummy.as and fill it with this:

    package
    {
        public class Dummy
        {
            public static function send(url:String):void
            {
                var request:Myclass=new Myclass(url);
            }
        }
    }
    class Myclass
    {
        public function Myclass(inp:String)
        {
            trace(inp);
        }
    }
    

    Again, get rid of

    <fx:Script source="URLRequest.as" />
    

    and replace it by

    <fx:Script>
        <![CDATA[
            import Dummy;
        ]]>
    </fx:Script>
    

    and finally replace

    <s:Button x="1" y="12" label="ɢɪᴛ push !" click="send()"/>
    

    with

    <s:Button x="1" y="12" label="ɢɪᴛ push !" click="Dummy.send(txtName.text)"/>
    

    The trick here is that we're importing the Dummy class that just has a static function which we can call without instantiating. Furthermore - as long as we define it outside of the package, we can add more class definitions, which are visible to the Dummy class.