如何在 ActionScript 类中实现数据绑定?

发布于 2024-07-10 12:46:45 字数 1539 浏览 7 评论 0原文

我在 ActionScript 组件中绑定值时遇到问题。 我基本上想将组件中变量的值设置为模型中的值,并在更新模型值时让组件变量自动更新。 我认为我只是不完全理解数据绑定在 Flex 中的工作原理 - 在使用 MXML 组件时这不是问题,但是在使用 ActionScript 类时,绑定不起作用。

这是我正在使用的代码,其中值没有绑定:

package
{
    public class Type1Lists extends TwoLists
    {
        public function Type1Lists()
        {
            super();

            super.availableEntities = super.composite.availableType1Entities;

            super.selectedEntities = super.composite.selectedType1Entities;
        }
    }
}

package
{
    public class Type2Lists extends TwoLists
    {
        public function Type2Lists()
        {
            super();

            super.availableEntities = super.composite.availableType2Entities;

            super.selectedEntities = super.composite.selectedType2Entities;
        }
    }
}

/* TwoLists.mxml */
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
        <![CDATA[
            public var __model:ModelLocator = ModelLocator.getInstance();

            public var composite:Composite = 
                __model.selectedComposite;

            [Bindable]
            public var availableEntities:ArrayCollection;

            [Bindable]
            public var selectedEntities:ArrayCollection;
        ]]>
    </mx:Script>

    <mx:List id="availableEntitiesList" dataProvider="{availableEntities}" />

    <mx:List id="selectedEntitiesList" dataProvider="{selectedEntities}" />
</mx:HBox>

I am having a problem with binding values in my ActionScript components. I basically want to set the value of a a variable in my component to a value in the model, and have the component variable automatically update when the model value is updated. I think that I just don't fully understand how data binding works in Flex - this is not a problem when using MXML components, but, when using ActionScript classes, the binding does not work.

This is the code I'm using, where the values are not binding:

package
{
    public class Type1Lists extends TwoLists
    {
        public function Type1Lists()
        {
            super();

            super.availableEntities = super.composite.availableType1Entities;

            super.selectedEntities = super.composite.selectedType1Entities;
        }
    }
}

package
{
    public class Type2Lists extends TwoLists
    {
        public function Type2Lists()
        {
            super();

            super.availableEntities = super.composite.availableType2Entities;

            super.selectedEntities = super.composite.selectedType2Entities;
        }
    }
}

/* TwoLists.mxml */
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
        <![CDATA[
            public var __model:ModelLocator = ModelLocator.getInstance();

            public var composite:Composite = 
                __model.selectedComposite;

            [Bindable]
            public var availableEntities:ArrayCollection;

            [Bindable]
            public var selectedEntities:ArrayCollection;
        ]]>
    </mx:Script>

    <mx:List id="availableEntitiesList" dataProvider="{availableEntities}" />

    <mx:List id="selectedEntitiesList" dataProvider="{selectedEntities}" />
</mx:HBox>

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

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

发布评论

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

评论(3

森林很绿却致人迷途 2024-07-17 12:46:45

要通过代码使用绑定,您应该使用 mx.binding.utils.*

看一下以及 BindingUtils.bindProperty 和 bindSetter 方法。

另外,请小心手动数据绑定,它可能会导致内存泄漏。
为了避免它们,保存bindProperty和bindSetter返回的ChangeWatcher,并在不再使用时调用watcher的unwatch方法(即在dipose或析构函数方法中)

To use binding by code you should use mx.binding.utils.*

Take a look and the BindingUtils.bindProperty and bindSetter methods.

Also, be careful with manual databinding, it could lead you to memory leaks.
To avoid them, save the ChangeWatcher returned by bindProperty and bindSetter and call watcher's unwatch method when is no more used (i.e, in the dipose or destructor method)

酒儿 2024-07-17 12:46:45

您需要将 [Bindable] 标记添加到类本身(使所有属性可绑定)或您想要成为 [Bindable] 的属性。 在 MXML 中将属性或对象标记为 [Bindable] 是不够的。

You need to add the [Bindable] tag either to the class itself (making all properties bindable) or the properties you want to be [Bindable]. Marking properties or objects as [Bindable] in your MXML is not sufficient.

GRAY°灰色天空 2024-07-17 12:46:45

为了解决这个问题,我只需将类转换为 MXML 组件,并为 ModelLocator 添加一个私有变量。

/* Type1Lists.mxml */
<?xml version="1.0" encoding="utf-8"?>
<TwoLists xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns="*" 
    availableEntities="{__model.selectedComposite.availableType1Entities}" 
    selectedEntities="{__model.selectedComposite.selectedType1Entities}">
    <mx:Script>
        <![CDATA[
            import model.ModelLocator;

            [Bindable]
            private var __model:ModelLocator = ModelLocator.getInstance();
    </mx:Script>
</TwoLists>

/* Type2Lists.mxml */
<?xml version="1.0" encoding="utf-8"?>
<TwoLists xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns="*" 
    availableEntities="{__model.selectedComposite.availableType2Entities}" 
    selectedEntities="{__model.selectedComposite.selectedType2Entities}">
    <mx:Script>
        <![CDATA[
            import model.ModelLocator;

            [Bindable]
            private var __model:ModelLocator = ModelLocator.getInstance();
    </mx:Script>
</TwoLists>

To fix this, I simply converted the classes to MXML components, and added a private variable for my ModelLocator.

/* Type1Lists.mxml */
<?xml version="1.0" encoding="utf-8"?>
<TwoLists xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns="*" 
    availableEntities="{__model.selectedComposite.availableType1Entities}" 
    selectedEntities="{__model.selectedComposite.selectedType1Entities}">
    <mx:Script>
        <![CDATA[
            import model.ModelLocator;

            [Bindable]
            private var __model:ModelLocator = ModelLocator.getInstance();
    </mx:Script>
</TwoLists>

/* Type2Lists.mxml */
<?xml version="1.0" encoding="utf-8"?>
<TwoLists xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns="*" 
    availableEntities="{__model.selectedComposite.availableType2Entities}" 
    selectedEntities="{__model.selectedComposite.selectedType2Entities}">
    <mx:Script>
        <![CDATA[
            import model.ModelLocator;

            [Bindable]
            private var __model:ModelLocator = ModelLocator.getInstance();
    </mx:Script>
</TwoLists>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文