Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think what your are asking is how to keep your actionscript files separate from the MXML files that contain your designs. The answer is simple:</p> <p>Create your actionscript file. Include only methods in this file, and do not wrap the code in a <code>package</code> or <code>class</code> definition. The file should look like the following:</p> <pre><code>import mx.controls.Alert; // ActionScript file /** * * Created By jviers * Created on Apr 14, 2011 * * */ public function hello():Alert{ Alert.show("Hello World!"); } </code></pre> <p>Create your MXML file that contains your "Design" components. Create a <code>Script</code> element on this mxml file and set the source to the relative path of your ActionScript file. The MXML should look like this:</p> <pre><code>&lt;?xml version = "1.0" encoding = "utf-8"?&gt; &lt;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 = "955" minHeight = "600"&gt; &lt;fx:Script&gt; &lt;![CDATA[ protected function button1_clickHandler ( event : MouseEvent ) : void { // TODO Auto-generated method stub hello (); } ]]&gt; &lt;/fx:Script&gt; &lt;fx:Declarations&gt; &lt;!-- Place non-visual elements (e.g., services, value objects) here --&gt; &lt;/fx:Declarations&gt; &lt;fx:Script source = "./scratch/MyFile.as" /&gt; &lt;s:Button label = "Show Alert" click = "button1_clickHandler(event)" /&gt; &lt;/s:Application&gt; </code></pre> <p>You'll notice that the ActionScript in the <code>scratch.MyFile.as</code> is executed when the application is run.</p> <p>You can use this method to include your drawing logic in your application. The external ActionScript is treated as if it were the method definitions for the class generated by the MXML.</p> <p>Let me caution you before everyone jumps all over me. This is <strong>NOT</strong> a best practice. There are specific use-cases in which to use this feature of Flex. Before I get to them let me explain why your notion of keeping "logic" separate from your "view" is inaccurate.</p> <p>Flex MXML files are not view-only code. They are a declarative dialect that simplifies ActionScript class definitions. When a Flex Project is compiled using mxmlc or compc (those are the compiler programs for flex applications and component libraries, respectively), MXML files are pre-compiled into ActionScript class definitions. If you add the <code>-keep-generated-actionscript</code> directive to your compiler options in Flash/Flex Builder/Ant/command line compile command, the compiler will leave the generated classes that compose the ActionScript class derived from the declaritive MXML files in your project. Thus, an MXML file becomes a class.</p> <p>Having ActionScript defined in a Script block in your MXML is <strong>NOT</strong> mixing "logic" with "presentation". Likewise, MXML does <strong>NOT</strong> define the presentation of your project. It simply is a declarative subset of the ActionScript language that makes it easier to define presentation classes. As proof, you can define <strong>ArrayCollections</strong>, <strong>Strings</strong>, and <strong>Vectors</strong> in MXML. Those classes are data containers and have absolutely nothing to do with presentation.</p> <p>Now, the reason putting all your ActionScript in external files is <strong>not a good thing</strong> is that it makes maintaining your project a headache. Not only does someone have to look up your component defined in MXML, but now they have to hunt through the codebase to find the script named Logic.as relatively defined to your component.</p> <p>The use-cases in which including the external ActionScript via Script.source are the following:</p> <ol> <li>A group of predefined methods/properties are used in lots of components without change. Rather than copy-and-paste those methods into each component, you use the Script.source property to define the methods once and reference them throughout your application.</li> <li>Similar, but different from use-case-1: You require a mix-in. Like an interface, a mix-in is a set of methods that must be defined for the component to work, and those methods must be reusable and define explicit input parameters and output parameters. Unlike an interface, those methods can be of any namespace type: protected, final, public, internal, mx_internal, etc., and can must function bodies, i.e. have code inside the <code>{}</code> function block. For an example of a "mix-in" think of an enumerable object, an object that has next(), previous(), reset(), and iterator(), methods. When you want to iterate over the object's properties, you call iterator(), which returns an iterator object that calls next() and previous() to fetch the next and previous property values from the object. You can mix this functionality into all kinds of objects and use them usefully. Also, your included functionality is encapsuled within the classes in which it is included. It works like an include directive in AS3.</li> </ol> <p>Hope this helps both your perceptions of what "logic" and "presentation" are in Flex, and helps you solve your particular issue.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload