`
DavyJones2010
  • 浏览: 153756 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Flex: DataBinding in depth (Part One)

    博客分类:
  • Flex
阅读更多

1. One-way binding & Two-way binding

    1) One-way binding: Source  ->Destination, once source changed, destination will change accordingly.

    2) Two-way binding: Source<->Destination, once source changed, destination will change and vice versa.

Eg.

    1)One-way binding

<?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" minWidth="955" minHeight="600">
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<fx:Script>
		<![CDATA[
			[Bindable]
			public var value:String = "Hello World";
		]]>
	</fx:Script>
	
	<s:VGroup>
		<s:TextInput id="textInput" change="{value=textInput.text}"/>
		<s:Label text="{value}"/>
	</s:VGroup>
</s:Application>

 

    2) Two-way binding

<?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" minWidth="955" minHeight="600">
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<s:VGroup>
		<s:TextInput id="textInput1"/>
		<s:TextInput text="@{textInput1.text}"/>
	</s:VGroup>
</s:Application>

 

     3) Two-way binding 2

<?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" minWidth="955" minHeight="600">
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<fx:Script>
		<![CDATA[
			[Bindable]
			private var text:String = "";
		]]>
	</fx:Script>
	<s:VGroup>
		<s:TextInput text="@{text}"/>
		<s:TextInput text="@{text}"/>
	</s:VGroup>
	
</s:Application>

 

2. Five main techniques for user Data Binding.

    1) Using braces with MXML tags

    2) Using the fx:Binding tag

    3) Using the BindingUtils class

    4) Implicit and explicit data binding

    5) Custom metadata

    ps. The above examples are data binding with MXML tags.

 

3. Data Binding with MXML tags(Braces) in depth

    1) Four steps:

        1) Generated code

        2) Event listeners and handlers

        3) Error catching

        4) Meta data

    2) How compiler works?

        1) mxmlc: The mxmlc compiler generates many ActionScript classes (mxml -> as)

        2) compc: The compc compiler compiles the mxmlc-generated classes and your classes to create a swf file (as -> swf)

        ps. The generated code(.as) are not visiable by default. However, you can instruct the compiler to keep these files by adding a compiler argument -keep or -keep-generated-actionscript=true

         Eg.(In _DataBindingUpderTheHoodWatcherSetupUtil.as)

// writeWatcher id=3 shouldWriteSelf=true class=flex2.compiler.as3.binding.PropertyWatcher..
shouldWriteChildren=true
watchers[3] = new mx.binding.PropertyWatcher("textInput1", { propertyChange: true } , //..
writeWatcherListeners id=3 size=1 [ bindings[2] ], propertyGetter);
// writeWatcher id=4 shouldWriteSelf=true class=flex2.compiler.as3.binding.PropertyWatcher..
shouldWriteChildren=true
watchers[4] = new mx.binding.PropertyWatcher("text", { textChanged: true, change: true }, //..
writeWatcherListeners id=4 size=1 [ bindings[2] ], null);
// writeWatcher id=0 shouldWriteSelf=true class=flex2.compiler.as3.binding.PropertyWatcher..
shouldWriteChildren=true
watchers[0] = new mx.binding.PropertyWatcher("textInput2", { propertyChange: true }, //..
writeWatcherListeners id=0 size=1 [ bindings[0] ], propertyGetter);
// writeWatcher id=1 shouldWriteSelf=true class=flex2.compiler.as3.binding.PropertyWatcher..
shouldWriteChildren=true
watchers[1] = new mx.binding.PropertyWatcher("text", { textChanged: true, change: true }, //..
writeWatcherListeners id=1 size=1 [ bindings[0] ], null);
// writeWatcher id=2 shouldWriteSelf=true class=flex2.compiler.as3.binding.PropertyWatcher..
shouldWriteChildren=true
watchers[2] = new mx.binding.PropertyWatcher("text", { propertyChange: true }, //..
writeWatcherListeners id=2 size=1 [ bindings[1] ], propertyGetter);
         (In dataBindingUnderTheHood-generated.as)
if (_watcherSetupUtil == null)
{
var watcherSetupUtilClass:Object =
getDefinitionByName("_DataBindingUnderTheHoodWatcherSetupUtil");
watcherSetupUtilClass["init"](null);
}
_watcherSetupUtil.setup(this,
function(propertyName:String):* { return target[propertyName]; },
function(propertyName:String):* { return DataBindingUnderTheHood[propertyName]; },
bindings,
watchers);
mx_internal::_bindings = mx_internal::_bindings.concat(bindings);
mx_internal::_watchers = mx_internal::_watchers.concat(watchers);

    Comment: As you can see, the magic is actually a lot of code that the mxmlc compiler creates on your behalf. All the code you have seen gets added to your application swf and is an overhead added every time you initialize your application.

4. One and Two-ways Data Binding Using Binding tag

 

<?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" minWidth="955" minHeight="600">
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<s:VGroup>
		<s:TextInput id="textInput1"/>
		<s:TextInput id="textInput2"/>
	</s:VGroup>
	<fx:Binding source="textInput1.text"
			 destination="textInput2.text"
                         twoWay="false"/>
</s:Application>

    ps. You can use <mx:Binding> instead of <fx:Binding>, but it's depreciated.

 

5. Using the BindingUtils Class

    The two techniques you have looked at so far: braces in an MXML tag and the binding tag work well and easy to implement. However, there is overhead associated with these methods.  Additionally, you cannot "unbind" properties using these techniques. 

    There are two ways to use the BindingUtil class.

        1) bindProperty() method: The static method is used to bind a public property.

        2) bindSetter() method: The static method is used to bind a setter function.

    1). bindProperty() function signature:

 

public static function bindProperty(
site:Object, prop:String,
host:Object, chain:Object,
commitOnly:Boolean = false,
useWeakReference:Boolean = false):ChangeWatcher
//site: Represents the destination object.
//host: Represents the source object.
//commitOnly: Set to true in case the handler should be called only on committing change events;
set to false in case the handler should be called on both committing and non-committing change
events. Default is false.
//useWeakReference: Allows you to decide whether the reference to the host is strong or weak. A
strong reference (the default) prevents the host from being garbage collected. A weak reference
does not.
<?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/halo"
minWidth="1024" minHeight="768">
<fx:Script>
<![CDATA[
import mx.binding.utils.BindingUtils;
import mx.events.FlexEvent;
import mx.binding.utils.ChangeWatcher;

private var change:ChangeWatcher;
protected function preinitializeHandler(event:FlexEvent):void
{
    change = BindingUtils.bindProperty(simpleText, "text", textInput, "text");
}
protected function clickHandler(event:MouseEvent):void
{
    change.unwatch();
    change = null;
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout />
</s:layout>
<s:TextInput id="textInput" preinitialize="preinitializeHandler(event)" />
<s:SimpleText id="simpleText" />
</s:Application>

    2) bindSetter() function signature

public static function bindSetter(setter:Function, host:Object,
chain:Object,
commitOnly:Boolean = false,
useWeakReference:Boolean = false):ChangeWatcher
// setter: Bind a setter function to a bindable property or chain. Once the ChangeWatcher instance
is successfully created, the setter function is invoked with the value or the chain object.
// host: Represents the source object.
// chain: Represents the property name.
// commitOnly : Set to true in case the handler should be called only on committing change events;
set to false in case the handler should be called on both committing and non-committing change
events. Default is false.
//useWeakReference: Allows you to decide whether the reference to the host is strong or weak. A
strong reference (the default) prevents the host from being garbage collected. A weak reference
does not.

 

<?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/halo"
minWidth="1024" minHeight="768">
<fx:Script>
<![CDATA[
import mx.binding.utils.BindingUtils;
import mx.events.FlexEvent;
protected function preinitializeHandler(event:FlexEvent):void
{
BindingUtils.bindSetter(bindingSetter, textInput, "text");
}
private function bindingSetter(str:String):void
{
label.text = str;
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout />
</s:layout>
<s:TextInput id="textInput" preinitialize="preinitializeHandler(event)" />
<s:Label id="label" />
</s:Application>

    Comments: BindingUtils class methods are quicker than the MXML braces because the class doesn't generate all the additional code that the compiler added.

分享到:
评论

相关推荐

    Android代码-使用DataBinding的RecyclerView

    Android Data Binding RecyclerView Using Recyclerview with the new Android Data Binding framework. ... dataBinding { enabled = true } Modify your layout Remember to use your classes and packages ;-).

    DataBinding-ktx:DataBinding-ktx使声明数据绑定变得容易

    DataBinding-ktx使声明数据绑定变得容易。 在这里。 数据绑定中的问题 忘记在DataBinding中调用setLifecycleOwner 。 在Activity和Fragment中声明binding变量的方式有所不同。 在活动中,可以使用by lazy声明...

    DataBinding基本使用

    数据绑定(DataBinding)是Android开发中的一个库,它提供了在布局XML文件中直接与Java对象绑定的能力,简化了UI和业务逻辑之间的交互。通过DataBinding,开发者可以更直观地处理用户界面的数据更新,减少代码量,...

    DataBinding的使用与原理

    **DataBinding 概述** DataBinding 是 Android 开发中的一个库,它允许开发者将数据模型直接绑定到用户界面,从而简化了代码,提高了可读性和可维护性。这个框架是 Android 支持库的一部分,引入 DataBinding 可以...

    :dna:Android DataBinding工具包,用于通知数据从模型层到UI层的更改。-Android开发

    :dna:Android DataBinding套件,用于通知MVVM架构上从Model层到UI层的数据更改。 Bindables:dna:Android DataBinding工具包,用于通知从模型层到UI层的数据更改。 该库提供了DataBinding的基类(BindingActivity,...

    Android代码-DataBindingAdapter

    DataBinding RecyclerViewAdapter 中文版 README Free from writing adapters! NO MORE CLASSES! Super simple RecyclerView adapter using Data Binding Technology, no longer need to write any adapter! You don...

    DataBinding使用Android

    以下是一个简单的例子,展示如何在布局中使用DataBinding: ```xml &lt;layout xmlns:android="http://schemas.android.com/apk/res/android"&gt; name="user" type="com.example.User" /&gt; ...&gt; android:...

    Android支持DataBinding的RecyclerView通用Adapter

    总结起来,"Android支持DataBinding的RecyclerView通用Adapter"是一个高效的解决方案,它整合了`DataBinding`的便利性和`BRVAH`的实用性,为开发者提供了一个强大且易用的工具,帮助他们更好地管理和展示列表数据。...

    DataBindingDemo2

    在Android开发中,DataBinding是一种强大的工具,它简化了UI与数据模型之间的交互,使得代码更加清晰、可读性更强。这个"DataBindingDemo2"是基于MVVM(Model-View-ViewModel)架构模式的一个示例,展示了如何在实际...

    DataBindingDemo

    《Android DataBinding 深入解析与实战指南》 在Android开发中,DataBinding库提供了一种强大的方式来简化UI和数据模型之间的交互,极大地提高了代码的可读性和可维护性。本文将深入探讨DataBinding的功能、优势...

    DataBinding的探索

    在Android开发中,DataBinding库是Google推荐的一种用于简化视图和数据模型之间绑定的技术,它使得MVVM(Model-View-ViewModel)架构模式在Android应用中的实现更加便捷。本篇文章将深入探讨DataBinding的工作原理、...

    cxf-rt-databinding-jaxb-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-databinding-jaxb-3.0.1.jar; 赠送原API文档:cxf-rt-databinding-jaxb-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-databinding-jaxb-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-...

    绑定:简单的API实现DataBinding和ViewBinding。简单的API实现DataBinding和ViewBinding,欢迎star

    在Android开发中,数据绑定(DataBinding)和视图绑定(ViewBinding)是两种非常重要的技术,它们可以显著提升代码的可读性和可维护性,减少样板代码,并增强UI组件与业务逻辑之间的交互。本项目提供了简单的API实现...

    Android开发:10.DataBinding数据绑定技术解析.pdf

    Android开发:10.DataBinding数据绑定技术解析.pdf

    DataBinding中ViewStub例子

    在Android开发中,DataBinding库是Google推荐的一种用于简化视图和数据绑定的工具,它极大地减少了我们在Activity或Fragment中处理UI逻辑的代码量。ViewStub是一个轻量级的视图,它在默认情况下不可见,只有在被...

    Android-一些DataBinding相关的工具类

    在Android开发中,DataBinding库是一个非常重要的组件,它能够帮助开发者更加简洁、高效地处理UI与数据之间的绑定。这个名为"Android-一些DataBinding相关的工具类"的资源集,显然是为了提供一些辅助方法和扩展,以...

    DataBinding2使用手册

    ### DataBinding2使用手册 #### 一、简介 DataBinding2是基于JQuery的一个扩展库,主要用于简化HTML元素与后端数据之间的绑定过程。相比于早期版本,DataBinding2增加了许多新特性,使得开发者能够更加高效地处理...

    Bindables::dna:Android DataBinding套件,用于通知MVVM架构上从Model层到UI层的数据更改

    :dna: Android DataBinding工具包,用于通知数据从模型层到UI层的更改。 该库提供了DataBinding的基类(BindingActivity,BindingFragment,BindingViewModel), 并支持在没有可观察字段和LiveData的情况下通知...

    android官方数据绑定框架DataBinding使用(2)

    在Android开发中,DataBinding库是Google推出的一种官方数据绑定框架,它可以帮助开发者更方便地实现视图与数据模型之间的绑定,从而简化UI逻辑,提高代码的可读性和可维护性。本文将深入探讨DataBinding的基本用法...

Global site tag (gtag.js) - Google Analytics