I'm not event sure if my title really reflect what's going on here... I just realized that apparently I'm missing a fundamental principle in the way Flex is handling data (memory?) and I need some help to understand it.
I have the a MySQL table named friends that contains the following rows ;
MySQL table friends
+-----------+----------+
| firstname | lastname |
+-----------+----------+
| Mark | Smith |
| Andrew | Barnes |
+-----------+----------+
In the next example, I'm calling my friendsService.getAllFriends() three times and then, I assign the result to the DataProvider of three different DataGrids.
The second time I send a query to the database, I'm changing the firstname of Mark to Peter, only before assigning the result to the DataProvider. I'm not updating the database in any way, right? :-)
What I expect :
MyDataGrid1
+-----------+----------+
| firstname | lastname |
+-----------+----------+
| Mark | Smith |
| Andrew | Barnes |
+-----------+----------+
MyDataGrid2
+-----------+----------+
| firstname | lastname |
+-----------+----------+
| Peter | Smith | <---- Say hello to Peter :-)
| Andrew | Barnes |
+-----------+----------+
MyDataGrid3
+-----------+----------+
| firstname | lastname |
+-----------+----------+
| Mark | Smith |
| Andrew | Barnes |
+-----------+----------+
After calling the second query (The one where I switch Mark to Peter programmatically), It looks like all three results are binded in some way I don't understand... I obtain :
MyDataGrid1
+-----------+----------+
| firstname | lastname |
+-----------+----------+
| Peter | Smith |
| Andrew | Barnes |
+-----------+----------+
MyDataGrid2
+-----------+----------+
| firstname | lastname |
+-----------+----------+
| Peter | Smith |
| Andrew | Barnes |
+-----------+----------+
MyDataGrid3
+-----------+----------+
| firstname | lastname |
+-----------+----------+
| Peter | Smith |
| Andrew | Barnes |
+-----------+----------+
It looks like Flex is not really asking for new data, assuming that it already have the answer from friendsService.getAllFriends() ...
Any enlightment for me? :-)
THANKS !
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:friendsservice="services.friendsservice.*"
creationComplete="creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
private var _step:Number = 1;
protected function creationCompleteHandler(event:FlexEvent):void
{
getAllFriends();
}
protected function getAllFriends():void
{
getAllFriendsResult.token = friendsService.getAllFriends();
}
protected function getAllFriendsResult_resultHandler(event:ResultEvent):void
{
if (_step == 1) {
assignValuesToDataGrid(MyDataGrid1, event.result, _step);
dbg.text = "MyDataGrid1 has been loaded";
}
if (_step == 2) {
assignValuesToDataGrid(MyDataGrid2, event.result, _step);
dbg.text = "MyDataGrid2 has been loaded";
}
if (_step == 3) {
assignValuesToDataGrid(MyDataGrid3, event.result, _step);
dbg.text = "MyDataGrid3 has been loaded";
}
_step++;
}
protected function assignValuesToDataGrid(dg:DataGrid, result:Object, step:Number):void
{
if (step == 2)
result[0].firstname = "Peter";
dg.dataProvider = result;
}
]]>
</fx:Script>
<fx:Declarations>
<s:CallResponder id="getAllFriendsResult" result="getAllFriendsResult_resultHandler(event)"/>
<friendsservice:FriendsService id="friendsService"/>
</fx:Declarations>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Button label="Load next DataGrid" click="getAllFriends()"/>
<mx:DataGrid id="MyDataGrid1" width="100%" height="100">
<mx:columns>
<mx:DataGridColumn headerText="First name" dataField="firstname"/>
<mx:DataGridColumn headerText="Last name" dataField="lastname"/>
</mx:columns>
</mx:DataGrid>
<mx:DataGrid id="MyDataGrid2" width="100%" height="100">
<mx:columns>
<mx:DataGridColumn headerText="First name" dataField="firstname"/>
<mx:DataGridColumn headerText="Last name" dataField="lastname"/>
</mx:columns>
</mx:DataGrid>
<mx:DataGrid id="MyDataGrid3" width="100%" height="100">
<mx:columns>
<mx:DataGridColumn headerText="First name" dataField="firstname"/>
<mx:DataGridColumn headerText="Last name" dataField="lastname"/>
</mx:columns>
</mx:DataGrid>
<s:Label id="dbg" fontWeight="bold"/>
</s:WindowedApplication>