Chinaunix首页 | 论坛 | 博客
  • 博客访问: 518496
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类:

2009-05-17 22:43:59


Google上搜索 Flex Mixin 发现,Flex中有[Mixin]元标签和 使用Mixin的概念,而且貌似使用
方法及含义均不同,故整理如下,共同探讨!

[Mixin] Metadata Tag 元标签

该标签的主要作用是为应用的初始化工作创建一个静态代码块。该代码块会在Flex程序运行的
最初阶段(SystemManager初始化完毕,Application尚未初始化时调用),具体的流程如下
所示:

1>. 编写需要由SystemManager在其初始化完毕后调用的类,格式如下:
(注意:只有第一步是用户需要做的,以后的由Flex编译程序完成)
//MyMixinExample.as

[Mixin]
public class MyMixinExample{
    public static function init (systemManager:IFlexModuleFactory){
        //Your code here

    }
}


2>. 在使用mxmlc编译相应的MXML应用时,将这些由[Mixin]标签 标记的类注册在案。如果你
使用 -keep-generated-actionscript 选项的话,可以查看生成的
_XXX_mx_managers_SystemManager-generated.as 文件(其中XXX是你的MXML文件的
主文件名)。以下是部分相关代码:

//_XXX_mx_managers_SystemManager-generated.as


public class _MyApp_mx_managers_SystemManager
    extends mx.managers.SystemManager
    implements IFlexModuleFactory
{
    // ... 省略其他代码

    override public function info():Object
    {
        return {
            // ... 省略其他属性

            mixins: [ "MyMixinExample", ... ]
        }
    }
}


3>. 在代码运行时,SystemManager初始化完毕后,由SystemManager逐个调用这些类 的的静态
init方法,并把SystemManager当做参数传递进去。。这是由Flex框架中的SystemManager类的
代码实现,主要代码摘抄如下:
public class SystemManager extends MovieClip
                         implements IChildList, IFlexDisplayObject,
                         IFlexModuleFactory, ISystemManager, ISWFBridgeProvider
{
    // ... 省略其他代码

    mx_internal function docFrameHandler(event:Event = null):void
    {
        // ... 省略其他代码

     var mixinList:Array = info()["mixins"];
        if (mixinList && mixinList.length > 0)
        {
         var n:int = mixinList.length;
            for (var i:int = 0; i < n; ++i)
         {
         // trace("initializing mixin " + mixinList[i]);

         var c:Class = Class(getDefinitionByName(mixinList[i]));
         c["init"](this);
         }
        }
        // ... 省略其他代码

    }
    // ... 省略其他代码

}

参考:
http://blog.appdivision.com/2009/01/14/flex-using-the-mixin-meta-tag/

http://hydra1983.blog.163.com/blog/static/11175037200841611137654/#



Using Mixins
虽然ActionScript不支持多重继承,但Mixin使得可以为通常从其他class继承的自定义Class
添加额外功能。Mixin 提供一种简单的方式,使得可以将既存的class的方法添加到一个自定义
class,而不用使用继承机制。原理就是将既存Class的成员添加到自定义class的prototype
属性上。以下通过官网的例子解释说明:
// GeekStoreDepartment.as

// 类GeekStoreDepartment已经继承TreeDataProvider,但是还想使用EventDispatcher的

// 的方法,为了此目的,实现方式如下:

class GeekStoreDepartment implements mx.controls.treeclasses.TreeDataProvider {

  import mx.core.Application;
  import mx.events.EventDispatcher;

  // Step 1. 这个属性没有任何意义。仅是形成依赖关系,以便在使用该类的时候自动将

  // EventDispatcher类加载进来。

  static var evtDispatcher = mx.events.EventDispatcher;

  // Step 2. 创建一段静态代码,将GeekStoreDepartment.prototype属性传入

  // EventDispatcher.initialize()方法。

  static function Initialize() : Boolean {
    var p = GeekStoreDepartment.prototype;

    EventDispatcher.initialize(p);

    return true;
  }

  // Step 3. 这个属性没有任何意义。仅是调用上面定义的静态Initialize方法,以便执行

  // 其中的代码。

  static var mixit : Boolean = GeekStoreDepartment.Initialize();

  // Step 4. 声明要使用的来自于EventDispatcher的方法。

  var dispatchEvent: Function;
  var addEventListener: Function;
  var removeEventListener: Function;

  // Simplified array

  var products: Array = [
        {label:"Red"},
        {label:"Green"},
        {label:"Blue"}
        ];

  // Custom DataProvider code would go here.


  // Minimal TreeDataProvider implementation.

  function hasChildNodes() : Boolean {return true;}

  function getChildNodes() : Array {return products;}

  function getProperty(name : String) {return products[name];}

  function setProperty(name : String, propertyValue, broadcastChange : Boolean) {
    products[0].label="White";
    // Step 5. 之后就可以实用这些来自于EventDispatcher的方法了。

    dispatchEvent( {type: "modelChange" } );
  }

  function isTreeDataProvider() : Boolean {
    return true;
  }

  function indexOf(item: Object): Number {
    for (var i=0; i&lt;products.length; i++) {
      if (products[i] == item) {
        return i;
      }
    }
  }

  // Stub out the rest of the TDP methods.

  function getData() : Object { }
  function setData(data : Object) {}
  function addTreeNode(arg,data) {}
  function addTreeNodeAt(index,arg,data) {}
  function removeTreeNodeAt(index) {}
  function removeTreeNode(){}
  function removeAll(){}
  function getTreeNodeAt(index : Number){}
}

参考:
http://www.adobe.com/support/documentation/en/flex/1/mixin/index.html

阅读(1456) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~