Sending ArrayCollections via P2P Flex

社会主义新天地 提交于 2019-12-13 18:08:57

问题


I have a question regarding P2P with flex. When passing data between two applications using P2P. I get the following error:

 warning: unable to bind to property 'piece' on class 'Object' (class is not an IEventDispatcher)


I've spent a few days using Google to try and find a solution, but a can't get rid of that error. I've tried using ObjectUtils, direct assignment, and creating a new ArrayCollection WITH the ObjectUtils inside the parenthesis and still can't solve the problem.


Purpose of code:
  -> Two users connect via P2P
  -> 1st user can manipulate pictures (stored as objects in the array collection).
  -> 1st user sends updated ArrayCollection (with changed pictures) to 2nd user
  -> 2nd user's ArrayCollection gets updated and now sees manipulated pics

As far as my knowledge of Flex goes (fairly new to it), I properly Binded what needed to be binded. Using pop-ups and trace, I was able to see that the data from the ArrayCollection gets copied in properly, but it just doesn't want to display.


Here are some snippets of my code:

[Bindable]
public var taken:ArrayCollection = new ArrayCollection ([
    new picLayout(1,'sky.png'),
    new picLayout(2,'bird.png')
    ])

public function receiveSomeData(pass:ArrayCollection):void
{   
    // Want to replace current version of variable "taken" with
    // the one passed in using P2P
    this.taken= new ArrayCollection(pass.source);
}

public function sendSomeData(free:ArrayCollection):void
{   
    sendStream.send("receiveSomeData",free);
}

<s:Button click="sendSomeData(taken)" label="Update" />

Thank You for your help and time!


回答1:


I figured out what the problem was and how to fix it - with partial thanks to these pages:
Unable to Bind warning: class is not an IEventDispatcher
Flex Warning: Unable to bind to property 'foo' on class 'Object' (class is not an IEventDispatcher)

I knew that the information was being successfully sent to the other peer, but the problem was that the objects INSIDE the ArrayCollection weren't made bindable.

My solution to the problem was as follows:

  • Create a loop that sends each object in the ArrayCollection along with an index that tells you what value in the ArrayCollection you are streaming.

  • Now, since you are "streaming" the data, overwrite the current ArrayCollection, using the setItemAt() function with first field as "new ObjectProxy(passedObject)" and the second field as the passedIndex (Note): the ObjectProxy() function forces the passed object to be bindable.


Here is an updated snippet of my code:

[Bindable]
public var takenPics:ArrayCollection = new ArrayCollection ([
    new picLayout(1,'sky.png'),
    new picLayout(2,'bird.png')
    ])

private function sendSomeData(data:Object, index:int):void
{   
    sendStream.send("receiveSomeData",data,index);
}

private function receiveSomeData(passedPic:Object,ix:int):void
{   
    // ObjectProxy needed to force a bindable object
    takenPics.setItemAt(new ObjectProxy(passedPic),ix);
}

public function sendPictures():void
{
    // ix < 2 because size of ArrayCollection is 2
    for (var ix:int = 0; ix<2; ix++)
        sendSomeData(takenPics[ix],ix);
}


来源:https://stackoverflow.com/questions/8252862/sending-arraycollections-via-p2p-flex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!