Asea: A lightweight AS3 module configuration and load Management library

Source: Internet
Author: User
Tags definition constant execution

Code is already hosted to Github:https://github.com/boycy815/asea

The library was originally designed to facilitate the configuration of RSL, and later found that the function gradually close to spring, so simply the function of a redesign of the ASEA, on the AS3 to achieve the basic IOC functions.

The following content allows you to learn how to use ASEA.

A. Hello World

Package
{
    import com.alibado.asea.*;
        
    Import Flash.display.Sprite;
        
    public class Aseatest extends Sprite
    {public
        function aseatest ()
        {
            //definition configuration XML
            var xml:xml = new XML ( <asea><trace value= "String/hello World"  /></asea>);
                
            Gets the root node processor
            var asea:eadrop = Eaconfig.getdrop ("Asea");
    
            Defines the context object
            var context:object = {};
    
            Execute
            asea.process (XML, [context]);}}
        

Unless you intend to extend it, it takes only four steps to use ASEA.

The commands you want to execute are defined in the configuration XML, and the specific syntax for the commands is described below.

The context object is the data environment in which the XML command is executed, in other words, you can fetch and write data from the context object in the XML command, and you can try to save the context object separately so that you can get the result of the processing. Here's a reminder that if your context object is not a dynamic object, then when you try to write a property that does not exist in the context environment on the XML, the system will complain (Referenceerror), and of course you don't need to do that.

The root node processor is a large oven that you give execution commands and contextual data to, and it handles it for you. The root node processor is obtained from the eaconfig, and it is generally not wrong to get the root node processor whose name is "Asea" unless you know exactly what label your root node is. The third parameter of the method process is a successful callback, which should be defined as a function (result:* = NULL): Void,result after processing, if your root node is "ASEA", then result is definitely null, The purpose of this callback is to consider that your XML might be handled asynchronously, such as loading a data in XML or something. The fourth parameter of the process is a callback after an error, defined as a function (Errorcode:int, message:string, Target:string, xml:xml): void.

Yes, you may also notice that the second parameter of the process is not directly a context object, but an array of only one element in the context. This problem involves the scope chain, and the role of the scope chain will be described later. I know this looks unfriendly, and I hope some good guy can wrap it up a bit.

Two. Label processor

ASEA currently provides the following tags: Asea,class,get,if,lib,method,new,selector,trace,with,bean.

You can find these definitions in the drops attribute of the Eaconfig class, drops is an array containing all the tag processors, and if you define your own label processor, you can push into the array to take effect.

All tag processors inherit eadrop This class, and you can define your own label processor simply by overwriting the Onprocess method and the Get name method. Another caveat is that the label processor is stateless, globally unique, so if your variable is not defined in the method body, be careful with it.

The simplest label processor is posted here.

Package Com.alibado.asea.drops
{
    import com.alibado.asea.EaDrop;
        
    public class Eaget extends Eadrop
    {
            
        override public function Get name (): String
        {return
            ' get ';
        }
            
            
        /**
         * Example:
         * <get id= "mypen" value= "pen" >
         /* override
        Protected function onprocess (DOM: XML, value:*, Contexts:array, oncomplete:function, onerror:function = null): void
        {
            oncomplete (value);
        }
    }
        
}

The Public function Get name (): String is overridden in Eaget to return the label name to be processed by the label processor in the method.

Also overwrite protected function onprocess (Dom:xml, value:*, Contexts:array, oncomplete:function, onerror:function = null): Void, the first parameter of the method is the XML that is processed, and the system guarantees that the root section of the XML is the name specified in Get name, and the second parameter value is the data that is queried in the context of the Value property in the label. If you don't want to know the results of a query and you want to handle the Value property yourself, you go to the XML. @value; contexts is the context; OnComplete is a successful callback, if your label processor produces results, Then pass the results to the oncomplete, if your processing does not produce results, then call OnComplete, because the entire processing chain is dependent on the oncomplete, if your tag has an id attribute and you pass the result, The system will automatically save your results where the id attribute is specified, OnError is the callback after the error is handled, and the definition is already mentioned above. Unless it is a particularly serious error, we do not recommend that OnComplete be invoked because of an error. Because if you do not call OnComplete, the system will assume that your label has not been processed and the entire process will not continue.

Three. Operation of context Environment

The context environment is an array in which the object labeled 0 is the "current context", and the object at the end of it is the "root context", and the "root context" is generally provided by us as the context object defined in the Hello World program. Other context objects are generated when you run XML, and as to why and when they are generated, these issues are explained in the specific introduction tag below.

With context, we need to read and write data in the context, and the Value property of the tag to read the data from the context.

The number of reads can be divided into constants and variables, and constants are grouped into String,number,boolean three types, such as "String/hello World" is a string constant, "number/110" is a numeric constant, and "Boolean/true" is a Boolean constant, The rest of the writing is counted as variables.

@root is a keyword that represents the root context, we can access the properties of the root context in a @root.attr way, or we can @root.attr.attr2 further. @this is also a keyword that represents the current context environment. If you do not use keywords, such as myball.x, the system first searches the current context for the Myball attribute, and if not, then searches up one level until the root context is found, similar to the property access rules for JS.

The id attribute can be used to save the results of the execution of the label, and if the ID is myball.x, the system will first search the Myball attribute in the context, locate the Myball object and set the X property to it. If the Myball property does not exist, the Myball property is established in its current context object and the X property is set. The same myball.positon.x words, if the myball does not exist, then establish the positon, then set up X. ID settings also support @root and @this, if the ID is directly equal to @this or @root, then you can directly overwrite the context object. It's quite dangerous but useful.

Four. Detailed label

ASEA: The most important of all tags, which properties do not support, but it is the normal operation of other tags in the container, it is generally as a root node or other tag processor as the parent class;

Bean:

/**
         * Example:
         * <bean id= "Mybean" >
         *     <new id= "NEWOBJ" value= "Pic" >
         *         <get value= "box"   />
         *         <get value= "number/400"   />
         * <get         value= "number/300"   />
         *         <get value= "String/this are my title"   />
         *     </new>
         *     <get id= "newobj.a" value= "box"/> * <get id= "newobj.b" value= "number/400     "   />
         *     <get id= "newobj.c" value= "number/300"   />
         * <get     id= "NEWOBJ.D" value= "String/this is My title "   />
         * </bean> *
         

Defines the definition of an object (not directly executing internal code), the first internal label must be a new label, the current context object is an empty object before the new label executes, and the current context object becomes the new object after the new label. Then the code can set the properties of new objects such as the operation ... The result of the Bean label execution is an object definition that executes the code inside the corresponding bean tag and gets the constructed content each time it is accessed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.