`
longgangbai
  • 浏览: 7356686 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Flex中一个类似Java的反射的工具类BindingUtils

阅读更多

BindingUtils的工具类的源代码:

此类用于绑定对象和组件的关系,减少在使用的多余累赘的代码:

 

package mx.binding.utils
{

import mx.binding.utils.ChangeWatcher;

/**
*  
 */
public class BindingUtils
{
    include "../../core/Version.as";

    //--------------------------------------------------------------------------
    //
    //  Class methods
    //
    //--------------------------------------------------------------------------

    /**
     *  Binds a public property, <code>prop</code> on the <code>site</code>
     *  Object, to a bindable property or property chain.
     *  If a ChangeWatcher instance is successfully created, <code>prop</code>
     *  is initialized to the current value of <code>chain</code>.
     *
     *  @param site The Object defining the property to be bound
     *  to <code>chain</code>.
     *
     *  @param prop The name of the public property defined in the
     *  <code>site</code> Object to be bound.
     *  The property will receive the current value of <code>chain</code>,
     *  when the value of <code>chain</code> changes.
     *
     *  @param host The object that hosts the property or property chain
     *  to be watched.
     *
     *  @param chain A value specifying the property or chain to be watched.
     *  Legal values are:
     *  <ul>
     *    <li>String containing the name of a public bindable property
     *    of the host object.</li>
     *
     *    <li>An Object in the form:
     *    <code>{ name: <i>property name</i>, getter: function(host) { return host[<i>property name</i>] } }</code>.
     *    This Object must contain the name of, and a getter function for,
     *    a public bindable property of the host object.</li>
     *
     *    <li>A non-empty Array containing a combination of the first two
     *    options that represents a chain of bindable properties accessible
     *    from the host.
     *    For example, to bind the property <code>host.a.b.c</code>,
     *    call the method as:
     *    <code>bindProperty(host, ["a","b","c"], ...)</code>.</li>
     *  </ul>
     *
     *  <p>Note: The property or properties named in the <code>chain</code> argument
     *  must be public, because the <code>describeType()</code> method suppresses all information
     *  about non-public properties, including the bindability metadata
     *  that ChangeWatcher scans to find the change events that are exposed
     *  for a given property.
     *  However, the getter function supplied when using the <code>{ name, getter }</code>
     *  argument form described above can be used to associate an arbitrary
     *  computed value with the named (public) property.</p>
     *
     *  @param commitOnly Set to <code>true</code> if the handler
     *  should be called only on committing change events;
     *  set to <code>false</code> if the handler should be called
     *  on both committing and non-committing change events.
     *  Note: the presence of non-committing change events for a property
     *  is indicated by the <code>[NonCommittingChangeEvent(&lt;event-name&gt;)]</code>
     *  metadata tag.
     *  Typically these tags are used to indicate fine-grained value changes,
     *  such as modifications in a text field prior to confirmation.
     *
     *  @return A ChangeWatcher instance, if at least one property name has
     *  been specified to the <code>chain</code> argument; null otherwise.
     */
    public static function bindProperty(
                                site:Object, prop:String,
                                host:Object, chain:Object,
                                commitOnly:Boolean = false):ChangeWatcher
    {
        var w:ChangeWatcher =
            ChangeWatcher.watch(host, chain, null, commitOnly);
       
        if (w != null)
        {
            var assign:Function = function(event:*):void
            {
                site[prop] = w.getValue();
            };
            w.setHandler(assign);
            assign(null);
        }
       
        return w;
    }

    /**
     *  Binds a setter function, <code>setter</code>, to a bindable property
     *  or property chain.
     *  If a ChangeWatcher instance is successfully created,
     *  the setter function is invoked with one argument that is the
     *  current value of <code>chain</code>.
     *
     *  @param setter Setter method to invoke with an argument of the current
     *  value of <code>chain</code> when that value changes.
     *
     *  @param host The host of the property.
     *  See the <code>bindProperty()</code> method for more information.
     *
     *  @param name The name of the property, or property chain.
     *  See the <code>bindProperty()</code> method for more information.
     *
     *  @param commitOnly Set to <code>true</code> if the handler should be
     *  called only on committing change events.
     *  See the <code>bindProperty()</code> method for more information.
     *
     *  @return A ChangeWatcher instance, if at least one property name
     *  has been  specified to the <code>chain</code> argument; null otherwise.
     */
    public static function bindSetter(setter:Function, host:Object,
                                      chain:Object,
                                      commitOnly:Boolean = false):ChangeWatcher
    {
        var w:ChangeWatcher =
            ChangeWatcher.watch(host, chain, null, commitOnly);
       
        if (w != null)
        {
            var invoke:Function = function(event:*):void
            {
                setter(w.getValue());
            };
            w.setHandler(invoke);
            invoke(null);
        }
       
        return w;
    }
}

}

 

 

使用简介:

    public static function bindProperty(
                                site:Object, prop:String,
                                host:Object, chain:Object,
                                commitOnly:Boolean = false):ChangeWatcher

site:绑定的组件

prop:绑定组件一个属性

host:绑定组件的一个对象

chain: 绑定组件对象host的一个属性

 

 

  public static function bindSetter(setter:Function, host:Object,
                                      chain:Object,
                                      commitOnly:Boolean = false):ChangeWatcher

setter:绑定一个方法

host:绑定属性的对象

name:绑定的属性

 

将对象tickOrder一个属性ORDER_PROCESS_STATUS 绑定到组件ORDER_PROCESS_STATUS的selectedValue属性

实现双向绑定:

如下:

 BindingUtils.bindProperty(ORDER_PROCESS_STATUS, "selectedValue", ticOrder, "ORDER_PROCESS_STATUS");
    BindingUtils.bindProperty(ticOrder, "ORDER_PROCESS_STATUS", ORDER_PROCESS_STATUS, "selectedValue"); 

 

 

 

 

分享到:
评论

相关推荐

    Flex 3 基础教程4)

    此外,Flex 还提供 `BindingUtils` 工具类,用于执行动态绑定操作,例如 `BindingUtils.bindProperty(source, sourceProp, target, targetProp)`,这允许在运行时创建绑定。 总之,数据绑定是 Flex 3 中的核心特性...

    Flex 动态绑定BindingUtils.bindProperty

    在Flex开发中,动态绑定是将一个组件的属性值与另一个组件的属性值关联起来,使得当一个组件的属性发生变化时,另一个组件的相应属性也会自动更新。`BindingUtils.bindProperty`是Adobe Flex中用于实现这种动态绑定...

    Flex问题解决大全

    总的来说,Flex提供了一套强大的工具集,用于创建动态、交互的Web应用。理解和掌握这些知识点,能够有效地解决Flex开发过程中遇到的问题,提高开发效率和应用质量。在实际开发中,结合具体需求灵活运用这些技术,将...

    Flex AS3学习笔记总结

    Flex 是 Adobe 开发的 RIA(Rich Internet Applications)工具,包括 Flex3 SDK、Flex Builder 和服务器产品,如 Lifestyle Data Services、CodeFushion。Flex 应用程序用 Flash.swf 文件格式封装发布在 HTML 里面,...

    细细品味Flex——新Flex学习手册.pdf

    - **解释**:本章详细介绍了各种Flex组件,如Alert组件、按钮组件、分组组件、数据组件、文本组件以及布局组件,每个组件都有其独特的功能和用途。 #### 第3章:Flash Media Server 3.0 (简称FMS3) - **知识点**:...

    FLEX数据绑定四种方式

    BindingUtils是Flex提供的一个实用工具类,可以用于动态创建绑定关系。这种方式适用于在运行时不确定绑定关系的情况。 **使用方法** 例如,可以使用`BindingUtils.setSource`和`BindingUtils.addTarget`方法来创建...

    flex学习心得

    3. 在ActionScript中使用`BindingUtils`类来绑定属性。 此外,数据绑定符`{}`可以直接引用属性或方法,如`{obj.text.toUpperCase()}`。而`[]`符号则表示属性的变化会触发数据绑定事件。 #### 十三、绑定事件触发 ...

    Flex4.5常见问题总结

    ### Flex4.5常见问题总结 #### 一、Flex4.5组件开发模型与特性 ...综上所述,Flex4.5提供了丰富的工具和机制,帮助开发者构建高性能、高互动性的应用程序。理解并掌握这些核心概念,是高效使用Flex进行开发的基础。

    Flex Bind数据绑定实例代码

    在ActionScript 3.0中,Flex的数据绑定主要通过两个类来实现:mx.binding.Binding和mx.binding.utils.BindingUtils。Binding类用于创建和管理数据绑定,而BindingUtils提供了一些静态方法,方便进行数据绑定操作。 ...

    Flex入门教程[汇编].pdf

    5. 实现数据绑定和验证,包括使用BindingUtils类、Validator类和Form类等。 6. 使用Flex Builder开发工具,包括创建新项目、设计用户界面、编写代码和调试应用程序等。 此外,本教程还涵盖了一些高级话题,例如: ...

    FLEX内存释放优化原则

    - 利用FLEX提供的性能分析工具(如Flex Performance Profiler)可以帮助开发者找出潜在的内存泄漏问题。通过这些工具,开发者可以了解哪些资源未被正确释放,以及如何改进代码以提高内存管理效率。 #### 五、总结 ...

    Flex高级数据绑定实例(界面语言切换)

    本示例通过一个具体的代码片段来展示如何使用Flex中的高级数据绑定技术来实现这一功能。该示例不仅涵盖了基本的数据绑定原理,还涉及到了模型与视图之间的交互、事件监听等高级主题。 #### 核心概念解析 1. **模型...

    Flex3.0学习手册

    - **BindingUtils类**: 介绍了如何利用BindingUtils类来进行动态数据绑定。 8. **样式与皮肤** - **样式**: 使用mx:Style标签定义局部样式或引入外部样式表。 - **皮肤**: 通过外部皮肤文件美化应用程序界面。 ...

    flex页面跳转及数据绑定

    4. **BindingUtils动态绑定**:在ActionScript代码中,可以使用BindingUtils类的bindProperty或bindMethod方法进行动态绑定。这种方式提供了更大的灵活性,可以在运行时创建绑定。 总的来说,Flex页面跳转和数据...

    Flex数据绑定专题

    数据绑定是Flex应用程序开发中的核心概念之一,它指的是将一个对象中的数据与另一个对象中的数据建立连接的过程。通过数据绑定,开发者可以实现在应用程序的不同层级间高效地传输数据。一个典型的数据绑定包括三个...

    《Flex企业应用开发实战》学习笔记之Flex企业应用开发基础[收集].pdf

    3. 动态数据绑定,通过ActionScript的BindingUtils类在运行时创建绑定。 通过这些基本概念和技术,开发者可以构建复杂、交互性强的Flex企业应用,实现高效的数据管理和用户交互。了解并熟练掌握MXML和数据绑定,是...

    flex4_读书笔记

    【Flex4基本概念】 Flex4 是 Adobe 引入的一个重要的 Flex 开发框架版本,它引入了许多新特性和改进,特别是在组件模型、...无论是组件设计、事件处理还是数据绑定,Flex4 都为开发者提供了强大的工具和灵活的架构。

    FLEX动态加载Model

    - `BindingUtils`类提供了一些静态方法,如`bindProperty()`,用于建立数据绑定。 4. **优化策略** - 为了提高性能,可以使用缓存机制,将已经加载过的Model存储起来,下次需要时直接复用,避免重复加载。 - ...

    flex3 数据绑定 总结

    签的 target 属性。下面的例子展示了如何使用 `&lt;mx:Binding&gt;` 实现相同的效果: ```xml ...无论是简单的属性绑定,还是复杂的函数和对象绑定,Flex 3 都提供了丰富的工具和选项来满足各种需求。

    flex画直线 清除直线 多点折线

    这个示例代码展示了如何在Flex应用中实现用户通过鼠标绘制直线、清除直线以及多点折线的功能。以下是详细的知识点解析: 1. **Flex架构**:这个应用基于Adobe Flex框架,使用Spark组件库,如`s:Application`、`s:...

Global site tag (gtag.js) - Google Analytics