使用 arrayCollection,您只需使其可绑定到所有组件
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
[Bindable]
public var artistData:ArrayCollection = new ArrayCollection([
{artistName : "Paula", artistLikes : "0"},
{artistName : "Bob", artistLikes : "0"},
{artistName : "Arthur", artistLikes : "0"}
]);
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:DataGrid id="artists" dataProvider="{artistData}" width="100%" height="150">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="artistName" headerText="Artist Name"/>
<s:GridColumn dataField="artistLikes" headerText="Artist Likes"/>
</s:ArrayList>
</s:columns>
</s:DataGrid>
<components:CompA artistDataInCompA="{artistData}" height="100"/>
<components:CompB artistDataInCompB="{artistData}" height="100"/>
假设您在 comp A 中增加了您的喜欢(不要忘记在 arrayCollection 上调用 refresh())
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var artistDataInCompA:ArrayCollection;
protected function button_clickHandler(name:String):void
{
var index:Number = getItemIndexByProperty(artistDataInCompA, "artistName", name);
var numLikes:Number = artistDataInCompA[index].artistLikes;
// Here you update your PHP
// HTTPService...
// and the ArrayCollection that is binded all the way through all components
artistDataInCompA[index].artistLikes = numLikes + 1;
artistDataInCompA.refresh();
}
protected function getItemIndexByProperty(array:ArrayCollection, property:String, value:String):Number
{
for (var i:Number = 0; i < array.length; i++)
{
var obj:Object = Object(array[i])
if (obj[property] == value)
return i;
}
return -1;
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Button label="Add a like to Paula" click="button_clickHandler('Paula')"/>
<s:Button label="Add a like to Bob" click="button_clickHandler('Bob')"/>
<s:Button label="Add a like to Arthur" click="button_clickHandler('Arthur')"/>
现在做你在comp B中必须做的事情
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var artistDataInCompB:ArrayCollection;
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Label text="Paula has {artistDataInCompB.getItemAt(0).artistLikes} like(s)"/>
<s:Label text="Bob has {artistDataInCompB.getItemAt(1).artistLikes} like(s)"/>
<s:Label text="Arthur has {artistDataInCompB.getItemAt(2).artistLikes} like(s)"/>