PDA

View Full Version : Flex Updating an array.



ahmednuaman
May 30th, 2008, 07:02 AM
Let's say I've got this:



<mx:ButtonBar itemClick="clickHandler(event);">
<mx:dataProvider>
<mx:Array id="ArrayToFill">
<mx:String>Bob</mx:String>
</mx:Array>
</mx:dataProvider>
</mx:ButtonBar>


And I want to update the array 'ArrayToFill' via AS once the app has loaded. How do I do that?

amarghosh
May 30th, 2008, 07:14 AM
i haven't done much Flex'ing --- but i think this is what u want.

<mx:ButtonBar itemClick="clickHandler(event);" dataProvider="{arrayToFill}" creationComplete="onCreated()">
</mx:ButtonBar>
<mx:Script>
<![CDATA[
[Bindable]
public var arrayToFill:Array;
public function onCreated():void
{
//assign new array and fill it here.
//or if u wanna do it dynamically, u can write it in another method that can be called from outside;
}
]]>
</mx:Script>

ahmednuaman
May 30th, 2008, 07:16 AM
I see, awesome, thanks!

Stratification
May 30th, 2008, 12:18 PM
Actually you don't need to declare a variable for arrayToFill in your code, it's already there. MXML basically gets converted to ActionScript on compile. So just treat arrayToFill like it's already there and a public variable and go from there. Hopefully that makes sense.

amarghosh
May 31st, 2008, 12:09 AM
Actually you don't need to declare a variable for arrayToFill in your code, it's already there. MXML basically gets converted to ActionScript on compile. So just treat arrayToFill like it's already there and a public variable and go from there. Hopefully that makes sense.

u mean we can do something like this? but its giving me "access of undefined property arrayToFill" at compile time. am using Flex Builder 3;

Also, if that was the case, and i declare arrayToFill explicitly in the code, would it hide the automatically defined variable?

<mx:ButtonBar itemClick="clickHandler(event);" dataProvider="{arrayToFill}" creationComplete="onCreated()">
</mx:ButtonBar>
<mx:Script>
<![CDATA[
public function onCreated():void
{
arrayToFill = new Array(1, 2, 3);
}
]]>
</mx:Script>

ahmednuaman
May 31st, 2008, 06:55 AM
Is it better to use creationComplete within the <mx:Application> tag than locally with tags?

Stratification
June 1st, 2008, 01:17 AM
You still have to declare it somewhere. Your earlier code had it in the MXML, so you could have just used it. The later example doesn't have it declared anywhere. As far as creationComplete, it depends on what you want to fire on. I usually have mine on the Application tag though.

ahmednuaman
June 1st, 2008, 07:03 AM
Yeah seems like that's the better idea.

amarghosh
June 2nd, 2008, 12:06 AM
Okay, now i see what u r saying.