加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex4 用AS实现数据绑定

发布时间:2020-12-15 03:47:33 所属栏目:百科 来源:网络整理
导读:上一篇文章主要讲了Mxml中实现数据绑定的简单示例,此处将使用ActionScript来写一个数据绑定的示例。 一、准备一个Model。 package { public class TestObj { [Bindable] public var description:String; public function TestObj(description:String = "")

上一篇文章主要讲了Mxml中实现数据绑定的简单示例,此处将使用ActionScript来写一个数据绑定的示例。

一、准备一个Model。

package {
  public class TestObj {
    [Bindable]
    public var description:String;
    
    public function TestObj(description:String = "") {
      this.description = description;
    }
  }
}

?1、description属性上加入Bindable注释是为了告知编译器该变量可作数据绑定。

二、测试单向数据绑定

1、新建一个Mxml来使用Model。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  xmlns:s="library://ns.adobe.com/flex/spark"
  xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
  [Bindable]
  private var _testObj:TestObj = new TestObj("hello");
]]>
</fx:Script>
  <s:layout>
    <s:VerticalLayout paddingLeft="5" paddingTop="5"/>
  </s:layout>
  <s:TextInput id="textInput1" text="{_testObj.description}"
    focusOut="_testObj.description = textInput1.text;"/>
  <s:TextInput id="textInput2" text="{_testObj.description}"
    focusOut="_testObj.description = textInput2.text;"/>
</s:Application>

新建TestObj类型变量,初始化属性description为hello。

将testObj的description属性绑定到textInput的text属性上。

两个textinput使用移除焦点事件给另外一个赋值。


三、测试双向绑定。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  xmlns:s="library://ns.adobe.com/flex/spark"
  xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
  [Bindable]
	private var _testObj:TestObj = new TestObj("Hi");
]]>
</fx:Script>
  <s:layout>
    <s:VerticalLayout paddingLeft="5" paddingTop="5"/>
  </s:layout>
  <s:TextInput id="textInput1" text="@{_testObj.description}"/>
  <s:TextInput id="textInput2" text="@{_testObj.description}"/>
</s:Application>
新建一个测试Model,然后使用双向绑定的语法。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读