0

我在 flex 中有一个问题,这让我有点头疼!

我正在向 ArrayCollection 添加对象,但这样做时,即使没有发生绑定,另一个 ArrayCollection 也会获取这些更改。

我可以从调试中看到两个 AC 具有相同的地址,但我一生都无法弄清楚为什么。

我有两个数组集合:

model.index.rows //The main array collection

model.index.holdRows //The array collection that imitates the above

这种幻像数据绑定仅发生在循环中的第一次迭代中,而对于所有其他迭代,它只会写入一次。

这被证明很麻烦的原因是它在我的数据网格中创建了重复的条目。

public override function handleMessage(message:IMessage):void
    {

        super.handleMessage(message);
        if (message is IndexResponse)
        {
           var response:IndexResponse = message as IndexResponse;

            model.index.rows.removeAll();
            model.index.indexIsEmpty = response.nullIndex;

            if (model.index.indexIsEmpty !== true)
            {

                //Update the index model from the response. Note: each property of the row object will be shown in the UI as a column in the grid
                response.index.forEach(function(entry:EntryData, i:int, a:Array):void
                    {
                        var row:Object = { fileID: entry.fileID, dadName: entry.dadName };

                        entry.tags.forEach(function(tag:Tag, i:int, a:Array):void
                            {
                                row[tag.name] = tag.value;
                            });

                        model.index.rows.addItem(row);

                    });

              if(model.index.indexForNetworkView == true){

                model.index.holdRows.source = model.index.holdRows.source.concat(model.index.rows.source);
                model.index.indexCounter++;
                model.index.indexForNetworkView = false;
                controller.indexController.showNetwork(model.index.indexCounter);                   
              }
                model.index.rows.refresh();
                controller.networkController.show();                  
            }

        }

有没有其他遇到类似情况的人提出解决方案?

4

2 回答 2

0

我在这个 stackoverflow 线程中问了同样的问题,它解释了发生了什么,以及你能做些什么来解决它:

对一个变量的更改会传播到另一个变量

拉迪斯拉夫

于 2010-05-20T10:31:01.570 回答
0

在咨询了上述线程后,我解决了这个问题:

我在另一个类中设置了代码,如下所示:

model.index.rows = model.index.holdRows;

这似乎在整个代码中传播。相反,我将此行更改为:

model.index.rows = new ArrayCollection(model.index.holdRows.source.concat());

正如提到的线程中所建议的那样。这为我删除了重复的条目!:o)

于 2010-05-20T10:51:20.523 回答