开发者

Unwanted binding

开发者 https://www.devze.com 2022-12-27 05:00 出处:网络
The situation is simple. I have a datagrid that gets its data from a webservice. When data from the webservice is retrived it calls the following function:

The situation is simple. I have a datagrid that gets its data from a webservice.

When data from the webservice is retrived it calls the following function:

private function onListReg():void
{
    arrRegOld = WSAutoreg.list.lastResult as ArrayCollection;
    arrReg = WSAutoreg.list.lastResult as ArrayCollection;

    dgReg.dataProvider = autoreglist;
}

dgReg is the Datagrid. the arr variables are ArrayCollections defined like so:

private var arrRegOld:ArrayCollection = new ArrayCollection;

[Bindable]
private var arrReg:ArrayCollection = new ArrayCollection;

The intent is whe开发者_开发技巧n I hit a update button, it compares arrRegOld with arrReg and see if any values have changes. The problem is whenever I change values on the Datagrid it changes on both the dataProvider and on both ArrayCollections.

Does anyone know why is this happening? What should I do so that the binding applies only to one ArrayCollection?

Appreciate any tip. - Mike


Your lists are sharing the same objects, if you modify the first element from arrReg you will see the modification also in arrRegOld - it is not related to binding. You need to clone the objects. You have several choices:

a) Implement a clone method for your objects (recommended) b) Use a generic method like this one:

            private function clone(source:Object):*             
            {               
                var array:ByteArray=new ByteArray();                    
                array.writeObject(source);                  
                array.position=0;                   
                return(array.readObject());             
            }

and call arrRegOld = clone (arrReg); after arrReg = WSAutoreg.list.lastResult as ArrayCollection;

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号