如何在不使用事件的情况下将数据从一个组件传递到其他组件

时间:2013-08-21 08:42:50

标签: actionscript-3 flex flex4

这些天我对Flex进行了parctising。我只有2个问题:

a)有人能告诉我如何在不使用事件的情况下将数据从一个组件传递到其他组件吗?我知道使用可以发送数据的事件..

b)我想用灵活的移动应用程序,你能告诉我从哪里开始吗?

2 个答案:

答案 0 :(得分:0)

要问a,您可以使用回调或观察者而不是事件

<强>回调

假设我们有组件A和B,实例是a和b。使用回调将不需要上下文。

b.callback = a.handler;

b.doSth();


// component define
Class A {

   public function handler(param:*):void {//you can have your param here

   }

}

class B {


   private var _callback:Function;//here you can use a array to save multi callbacks.

   public function set callback(value:Function):void {
       _callback = value;
   }

   public function doSth():void {
       //do something

       if (_callback  != null) {
           _callback.apply(null, [some data]);
       }
   }
}

<强>观察

以下是对Observer的一些介绍,它看起来像是以某种方式回调。

Observer

答案 1 :(得分:0)

a)创建Singleton类。您可以在课程之间共享数据 喜欢这个

CustomSingleton.as

package modules.custom
{

public class CustomSingleton
{

    private var _g_data1: String;
    private var _g_data2: String;
    private var _g_object1: Object;

    public function CustomSingleton(internally:SingletonInternal)
    {
        super();
        if(internally == null)
        {
            throw new Error("Must use getInstance() Method.");
        }
    }
    public static function getInstance():CustomSingleton
    {
        return SingletonInternal.instance;
    }

    public function set g_data1(value:String):void
    {
        _g_data1 = value;
    }

    public function get g_data1():String
    {
        return _g_data1;
    }

    public function set g_data2(value:String):void
    {
        _g_data2 = value;
    }

    public function get g_data2():String
    {
        return _g_data2;
    }

    public function set g_object1(value:Object):void
    {
        _g_object1 = value;
    }

    public function get g_object1():Object
    {
        return _g_object1;
    }

}
}
import modules.custom.CustomSingleton;



class SingletonInternal{
public static var instance:CustomSingleton
    = new CustomSingleton(new SingletonInternal());
public function SingletonInternal(){}
}

Component1.as

public class Component1
{
     private var singleton: CustomSingleton = CustomSingleton.getInstance();
     public function hoge(): void
     {
          singleton.g_data1 = "aaa";
     }
}

Component2.as

public class Component2
{
     private var singleton: CustomSingleton = CustomSingleton.getInstance();
     public function hoge(): void
     {
          trace(singleton.g_data1);
          singleton.g_data1 = "bbb";
     }
}

b)创建Hello world应用程序。
http://blog.air4android.com/2011/07/10/hello-world-air-for-android/