Flex绑定两个变量

时间:2016-01-24 11:55:23

标签: flash flex data-binding

我不知道Flex / Flash,但我有遗留代码,它迫使我寻找答案。请帮帮我。

我有控制器和演示者课程。控制器可以加载一些配置,我在这里添加了布尔变量isConfigurationLoaded,现在我想在控制器中将此变量与presenter中的相同变量绑定。我怎么能这样做?

我读到了BindingUtils,但很多样本都有mxml文件的部分内容,这让我有点害怕。谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您需要在控制器中将变量声明为Bindable,以便BindingUtils在更改后获取事件:

[Bindable]
public var isConfigurationLoaded:Boolean;

然后在演示者中设置绑定:

// use weak references so it will be garbage collected
BindingUtils.bindProperty(this, "isConfigurationLoaded", myControllerReference, "isConfigurationLoaded", false, true);

public function set isConfigurationLoaded(value:Boolean):void
{
    trace("isConfigurationLoaded in the controller has changed to " + value);
    _isConfigurationLoaded = value;
}

public function get isConfigurationLoaded():Boolean
{
    return _isConfigurationLoaded;
}

或者,如果您不想为变量创建getter / setter,可以将变量绑定到演示者中的函数调用:

// use weak references so it will be garbage collected
BindingUtils.bindSetter(onisConficurationLoaded, myControllerReference, "isConfigurationLoaded", false, true); 

private function onisConficurationLoaded(value:String):void
{
    trace("isConfigurationLoaded in the controller has changed to " + value);
    isConficurationLoaded = value;
}

BindingUtils返回一个ChangeWatcher实例,您可以存储它并在需要时取消监视该属性:

// watch
var changeWatcher:ChangeWatcher = BindingUtils.bindSetter(onisConficurationLoaded, myControllerReference, "isConfigurationLoaded");

// unwatch
changeWatcher.unwatch();