绑定简单是 addeventlistener 的快捷方式

发布于 2024-11-06 01:53:40 字数 105 浏览 6 评论 0原文

我从来没有真正理解绑定的意义,除了它是 addeventlistener 的有效简写。

还有更多吗?我错过了什么吗?

谢谢, dsdsdsdsd dsdsdsdsd

I never really understood the point of binding, beyond it being effectively shorthand for addeventlistener.

is there more to it? am I missing something?

thanks,
dsdsdsdsd

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落叶缤纷 2024-11-13 01:53:40

数据绑定就是以声明方式定义数据在 UI 中的显示方式。在幕后,它有点复杂,因为除了挂钩 addEventListener 来支持数据绑定功能之外,还有更多的需求。

实际上,这是一个非常强大的功能,为了更多地理解它,我们可以看一个简单的“Hello World”应用程序:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark">
    <s:HGroup>
        <s:TextInput id="input" />
        <s:Label text="Hello {input.text}" />
    </s:HGroup>

</s:Application>

现在,使用 --keep 编译器标志编译此应用程序并查看新文件夹“bin-debug/ generated”。我们对 HelloWorld- generated.as 感兴趣,

这里是定义绑定并从构造函数调用的地方:

private function _HelloWorld_bindingsSetup():Array
{
    var result:Array = [];

    result[0] = new mx.binding.Binding(this,
        function():String
        {
            var result:* = "Hello " + (input.text);
            return (result == undefined ? null : String(result));
        },
        null,
        "_HelloWorld_Label1.text"
        );


    return result;
}

稍后,在 HelloWorld 构造函数中,您会收到一个设置观察者的调用:

        _watcherSetupUtil.setup(this,
                function(propertyName:String):* { return target[propertyName]; },
                function(propertyName:String):* { return HelloWorld[propertyName]; },
                bindings,
                watchers);

这实际上就是这样做的:

watchers[0] = new mx.binding.PropertyWatcher("input", 
                                            { propertyChange: true }, 
                                            [ bindings[0] ] , 
                                            propertyGetter );
watchers[1] = new mx.binding.PropertyWatcher("text",
                                             { change: true,
                                               textChanged: true },
                                             [ bindings[0] ],
                                             null);

事情变得更多双向绑定很复杂。

Data binding is all about declaratively defining how data is displayed in the UI. Under the hood, it is a bit more complicated, because there are more needs than just hooking addEventListener to support the features of data binding.

It is a very powerful feature, actually, and to understand it more, we can look at a simple "Hello World" application:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark">
    <s:HGroup>
        <s:TextInput id="input" />
        <s:Label text="Hello {input.text}" />
    </s:HGroup>

</s:Application>

Now, compile this app with the --keep compiler flag and look at the new folder "bin-debug/generated". We are interested in HelloWorld-generated.as

Here is where that binding gets defined and called from the constructor:

private function _HelloWorld_bindingsSetup():Array
{
    var result:Array = [];

    result[0] = new mx.binding.Binding(this,
        function():String
        {
            var result:* = "Hello " + (input.text);
            return (result == undefined ? null : String(result));
        },
        null,
        "_HelloWorld_Label1.text"
        );


    return result;
}

A little later, in the HelloWorld constructor, you get a call to set up the watchers:

        _watcherSetupUtil.setup(this,
                function(propertyName:String):* { return target[propertyName]; },
                function(propertyName:String):* { return HelloWorld[propertyName]; },
                bindings,
                watchers);

Which really just does this:

watchers[0] = new mx.binding.PropertyWatcher("input", 
                                            { propertyChange: true }, 
                                            [ bindings[0] ] , 
                                            propertyGetter );
watchers[1] = new mx.binding.PropertyWatcher("text",
                                             { change: true,
                                               textChanged: true },
                                             [ bindings[0] ],
                                             null);

Things get more complicated with two-way bindings.

醉梦枕江山 2024-11-13 01:53:40

Flex 4 中的数据绑定我想可以被描述为 addEventListener() 的快捷方式 - 但这有点像说汽车只是步行的快捷方式。如果您只是绕着街区走一走,没什么大不了的 - 但如果您正在构建一个复杂的应用程序,其中包含大量项目渲染器和大量可能随时变化的数据点,则数据绑定可以让您避免编写数百个 addEventListener () 和removeEventListener() 调用,以及它们关联的处理程序。在这种情况下,这确实是一件大事。

Data Binding in Flex 4 COULD I guess be described as a shortcut for addEventListener() - but that's a bit like saying that cars are just a shortcut for walking. If you're only going around the block, no big deal - but if you're building a complex application with lots of item renderers and lots of data points that can vary at a moment's notice, data binding lets you avoid writing hundreds of addEventListener() and removeEventListener() calls, as well as their associated handlers. It's kind of a really big deal, in that context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文