带有FLEX代码的函数参数

时间:2011-12-12 07:57:39

标签: flex actionscript

我在Flex手册中找到了MXML和ActionScript中的代码。问题是“val”变量应该作为参数传递给updateMyString()函数调用语句,但它不会在代码中发生。那是为什么?

<?xml version="1.0"?>
<!-- binding/BindSetterAS.mxml -->
<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">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import mx.binding.utils.*;
            import mx.events.FlexEvent;
            // Method called when myTI.text changes.
            public function updateMyString(val:String):void {
                myTA.text = val.toUpperCase();
            }
            <!-- Event listener to configure binding. -->
            public function mySetterBinding(event:FlexEvent):void {
                var watcherSetter:ChangeWatcher = 
                    BindingUtils.bindSetter(updateMyString, myTI, "text");
            }
        ]]>
    </fx:Script>
    <s:Label text="Bind Setter using setter method"/>
    <s:TextInput id="myTI" 
        text="Hello Setter" />
    <s:TextArea id="myTA" 
        initialize="mySetterBinding(event);"/>
</s:Application>

2 个答案:

答案 0 :(得分:0)

var watcherSetter:ChangeWatcher = BindingUtils.bindSetter(updateMyString, myTI, "text");

功能如下:

bindSetter(functionToCall, objectToBind, PropertyToBind)

所以,你实际上是绑定myTI.text。这作为functionToCall的参数传递,即updateMyString。知道了吗?

答案 1 :(得分:0)

文档说BindingUtils.bindSetter期望一个带有一个参数的setter函数:

  

setter:Function - 当值改变时用链的当前值的参数调用的Setter方法

在这种情况下,val参数将设置为链的当前值。您可以在此处阅读:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/binding/utils/BindingUtils.html#bindSetter

希望这有帮助!