views:

428

answers:

1

I have the following two projects in in Flex Builder 3:

  • One AS3 library project (generates a SWC file)

  • One Flex application project (MXML Application)

The MXML Application references to the AS3 library project (Flex build path). So far, so good. I now want to run code automatically when an application uses the AS3 library. The [mixin] tag should do exactly what I need.

I followed the instructions from http://nondocs.blogspot.com/2007/04/metadatamixin.html and checked out the AutoQuick project. The latter is an example project by Adobe showing the use of the automation framework. In this project they are using the [mixin] tag (class AQAdapter).

I followed the examples but my code is not working. The static init method is not called. I added the library to the compiler arguments list that didn't work either.

How do I get this to work?

/* class to be automatically loaded */
package {
    /* includes */
    [mixin]
    public class TestApp extends Sprite {
        /* additional members */


        private static var mContainer:DisplayObjectContainer;

        private static var mInstance:TestApp;

        /**
        *  @private    
        */
        public static function init(root:DisplayObject):void
        {
            if(!mInstance)
            {
                mContainer = root as DisplayObjectContainer;

                mContainer.addEventListener(FlexEvent.APPLICATION_COMPLETE, applicationCompleteHandler);
            }
        }
    }

}
A: 

With the [Mixin] tag, the static init() method will be called at application start-up, as long as the class is referenced directly or indirectly from the main application.

Also, you have to remember that this method is run in a static context, so you shouldn't reference methods or attributes that require an instance (non-static), without creating the instance first.

Link: http://adamflater.blogspot.com/2007/03/static-code-blocks.html

MizardX
The tag [Mixin] (first letter is upper case!) must be used and a reference like "var test:TestApp;" is sufficient to call the static code.
natorion
The compiler option -include=ClassName loads the class anyway, so it is not needed to add a reference in code.
natorion