AS3:如何知道 dataprovider 或其内容是否已更改

发布于 2024-10-11 04:36:34 字数 1303 浏览 5 评论 0原文

我正在实现某种组合框控件(通过扩展spark.components.supportClasses.DropDownListBase)

现在,在这个控件内;我需要知道:

  1. 数据提供者是否更改/分配。 (我可以做到......下面的第一种方法有效);
  2. 如果 dataprovider 集合中的任何项目发生更改。

我尝试了 2 种方法,但没有成功...

第一种方法:

        [Bindable("collectionChange")]
        override public function set dataProvider(value:IList):void
        {
            if (value) value.addEventListener(CollectionEvent.COLLECTION_CHANGE, onDataChange);

            super.dataProvider = value;
            trace("DATA CHANGED"); //fires
        }

        protected function onDataChange(event:CollectionEvent):void
        {
            trace("COLLECTION ITEM(S) CHANGED"); //does not fire
        }

第二种方法:

因为这是基于 DropDownListBase 的;它应该已经调度 CollectionEvent.COLLECTION_CHANGE 事件..?

        public function myClass() //constructor
        {
            addEventListener(CollectionEvent.COLLECTION_CHANGE, onDataChange);
        }

        protected function onDataChange(event:CollectionEvent):void
        {
            trace("DATA CHANGED"); //does not fire
        }

有什么想法吗?

更新:在上面编辑。第一种方法让我知道数据提供者是否更改,但不知道数据提供者集合中的任何项目是否更新。第二种方法根本行不通。。

I'm implementing some kind of combobox control (by extending spark.components.supportClasses.DropDownListBase)

Now, inside this control; I need to know:

  1. if the dataprovider is changed/assigned. (which I can do... the first approach below works);
  2. if any item in the dataprovider collection has changed.

I tried 2 methods that did not do the trick...

1ST APPROACH:

        [Bindable("collectionChange")]
        override public function set dataProvider(value:IList):void
        {
            if (value) value.addEventListener(CollectionEvent.COLLECTION_CHANGE, onDataChange);

            super.dataProvider = value;
            trace("DATA CHANGED"); //fires
        }

        protected function onDataChange(event:CollectionEvent):void
        {
            trace("COLLECTION ITEM(S) CHANGED"); //does not fire
        }

2ND APPROACH:

Since this is based on DropDownListBase; it should dispatch the CollectionEvent.COLLECTION_CHANGE event already..?

        public function myClass() //constructor
        {
            addEventListener(CollectionEvent.COLLECTION_CHANGE, onDataChange);
        }

        protected function onDataChange(event:CollectionEvent):void
        {
            trace("DATA CHANGED"); //does not fire
        }

Any ideas?

UPDATE: Edited above.. The first approach lets me know if the dataprovider is changed but not if any item is updated in the dataprovider collection. The second approach does not work at all..

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

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

发布评论

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

评论(1

明明#如月 2024-10-18 04:36:34

如果您向我们展示一个可运行的示例来演示该问题,我们将能够提供更多帮助。

  1. 如果数据提供者是
    更改/分配。

你的第一个方法应该有效。您能告诉我们是什么让您认为没有吗? (我假设没有跟踪语句?)。并告诉我们您对 dataProvider 进行了哪些更改。

第二种方法不起作用,因为 myClass 没有触发 collectionChange 事件。

2 如果 dataprovider 集合中的任何项目发生更改。

确实没有办法告诉这一点。在大多数情况下,集合只是指向其他对象的指针的列表。如果更改这些指针,则会触发 collectionChange 事件。如果您更改了指针所指向的项目,则集合无法知道某些内容发生了更改。如果您是 MXML 粉丝,则绑定的工作方式非常相似。

如果您可以控制项目的更改方式,您就可以以这种方式处理它。而不是:

(collection.getITemAt(x) as myObject).property = newValue;

做这样的事情:

  var myObject : MyObject = collection.getITemAt(x) as myObject
  myObject.property = newValue;
  collection.setItemAt(x, myObject);

我希望触发 collectionChange 事件,但不是前者。

也就是说,在 dropDownListBase 的上下文中:当您滚动或打开和关闭下拉列表时,应该更新 itemRenderers 以反映最新的 dataProvider 的数据。但是,如果您在下拉菜单打开时动态更改某些内容,我不希望它自动更新[如果您不更改 dataProvider。

We'll be able to help significantly more if you show us a runnable sample to demonstrate the problem.

  1. if the dataprovider is
    changed/assigned.

Your first approach should work. Can you tell us what makes you think it didn't? ( No trace statement I assume? ). And tell us what you did to change the dataProvider.

The second approach won't work because myClass is not firing off the collectionChange event.

2 if any item in the dataprovider collection has changed.

There is not really a way to tell this. In most cases, a collection is just a list of pointers to other objects. If you change those pointers, then a collectionChange event is fired. IF you change the item that he pointer is pointed to, the collection has no way to know that something changed. Binding works very similarly if your an MXML fan.

If you have control over how items are changed, you can deal with it that way. Instead of:

(collection.getITemAt(x) as myObject).property = newValue;

Do something like this:

  var myObject : MyObject = collection.getITemAt(x) as myObject
  myObject.property = newValue;
  collection.setItemAt(x, myObject);

I would expect that to fire a collectionChange event, but not the former.

That said, in the context of a dropDownListBase: As you scroll or open and close the drop down, the itemRenderers should be updated to reflect the most current dataProvider's data. But if you change something on the fly while the drop down is open, I would not expect it to update automatically [if you're not changing the dataProvider.

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