简体   繁体   English

Flex条件数据绑定

[英]Flex conditional data binding

I have got two labels in my flex mxml component. 我的flex mxml组件中有两个标签。

first one shows playheadtime of videodisplay and another is also used for same purpose. 第一个显示视频显示的播放时间,另一个也用于相同目的。
the difference is that when we are in add mode(decided from a flag variable) 区别在于,当我们处于添加模式时(由标志变量决定)
both should show current playheadtime using binding. 两者都应使用绑定显示当前的播放时间。

but when we are in edit mode(again decided from flag) the latter label should remain static, to be more specific, the value retrived from database. 但是,当我们处于编辑模式(再次由标志决定)时,后一个标签应保持静态,更具体地说,应从数据库中检索该值。

how can I do that using actionscript. 我该如何使用动作脚本做到这一点。 I tried ChangeWathcer but I found it a bit tricky. 我尝试过ChangeWathcer,但发现它有些棘手。 Is there any other simpler way or am I missing something. 还有其他更简单的方法吗,还是我错过了一些东西。

following is my code. 以下是我的代码。

private function init():void 私有函数init():void
{ {
if (queFlag == 'a') 如果(queFlag =='a')
{ {
// timeLbl.text = currentTimeLbl.text using some binding mechanism //使用某种绑定机制,将timeLbl.text = currentTimeLbl.text
} }
else if(queFlag == 'e') 否则if(queFlag =='e')
{ {
// timeLbl.text = 'value retrived from database' ; // timeLbl.text ='从数据库中获取的值';
} }

} }

here currentTimeLbl shows videoDisplay playheadtime so it changes dynamically as video plays. 这里currentTimeLbl显示videoDisplay播放时间,因此它会随着视频播放而动态变化。

please help me out. 请帮帮我。

You could do it in something like the following: 您可以通过以下方式进行操作:

<Label id="timeLbl" text="{timeLabelText}"/>

<Label id="currentTimeLbl" change="dispatchEvent('currentTimeLblChanged')"/>

[Bindable(event = "queFlagChanged")] 
[Bindable(event = "currentTimeLblChanged")]
private function get timeLabelText():String   
{
    if(_queFlag == 'a')
    {
        return currentTimeLbl.text;
    }
    else
    {
        return 'value retrived from database';
    }        
}

public function set queFlag(value:String):void
{
    _queFlag = value;

    dispatchEvent(new Event("queFlagChanged"));
}

Here is a very short way of conditional binding in Flex. 这是Flex中条件绑定的一种非常简短的方法。 If you code the conditions into MXML curly-bracket-bindings they will be transformed by the MXML compiler to listeners on all objects participating in this expression. 如果将条件编码为MXML弯括号绑定,则MXML编译器会将其转换为参与此表达式的所有对象的侦听器。

Here is a working example: 这是一个工作示例:

<mx:CheckBox id="flagBox"/>
<mx:ComboBox dataProvider="{['a','e']}" id="flagBox2"/>
<mx:TextInput id="txtBox"/>
<mx:Label text="default: {txtBox.text}"/>
<mx:Label text="conditional (bool): { (flagBox.selected)? txtBox.text: 'default' }"/>
<mx:Label text="conditional (value): { (flagBox2.selectedItem == 'a')? txtBox.text: 'default' }"/>
  1. Checking flagBox will result in label #2 displaying "default" otherwise the text from the txtBox is displayed. 检查flagBox将导致标签#2显示“默认”,否则将显示txtBox的文本。
  2. Selecting "a" in flagBox2 will result in label #3 displaying "default" otherwise the text from the txtBox is displayed. flagBox2选择“ a”将导致标签#3显示“默认”,否则将显示txtBox的文本。

I regularly use this for reducing my lines of code in my UI-logic and it works quite well for me. 我经常使用它来减少我的UI逻辑中的代码行,并且对我来说效果很好。 A problem of this techniques is that you can't use all logic symbols in curly-braket-bindings, such as < or && , but i usually could life with that. 这种技术的一个问题是您不能在花括号绑定中使用所有逻辑符号,例如<&& ,但是我通常可以忍受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM