Showing posts with label ActionScript. Show all posts
Showing posts with label ActionScript. Show all posts

Wednesday, May 5, 2010

Flex Actionscript GoodNess :Changing States in Flex from ActionScript

States are a breeze in Flex4, they are so elegant and simple to use.

If you are using Cairngorm or other model, you are bound to change the State in Flex from Command Object through actionScript.

Here is how to change the states..

e.g
import mx.core.FlexGlobals

//then Navigate to the component in a top down hierarchy and change the //currentState property

FlexGlobals.topLevelApplication.currentState="AdvancedSearch"


Done!! Hurray!!

Flex Actionscript GoodNess: Updating Datagrid Display when Model(ArrayCollection) changes

Normally, making the dataProvider bindable makes sure, that if the underlying VO(Value Object) changes, the changes are reflected in the dataGrid.
But In case if the VO used cannot be made Bindable it becomes a problem, for e.g if the VO is a Dictionary Object.
In that case we need to notify the listeners of ArrayCollection that one of the VOs has changed, following code demostrates that.

[Bindable]
var myDataArray:ArrayCollection = new ArrayCollection()

Lets Create the VOs to add to this collection
var empArr:Array = new Array()
var emp1:Dictionary = new Dictionary()
emp1.name ="john"
emp1.lastName="rick"
empArr.push(emp1)

var emp2:Dictionary = new Dictionary()
emp2.name ="richard"
emp2.lastName="martin"
empArr.push(emp2)

myDataArray = new ArrayCollection(empArr)

//Now lets set the dataProvider to the DataGrid


Now lets say if I go and update the emp1 Object, the value needs to get updated in
the DataGrid, but it will not unless I call itemUpdated() on the ArrayCollection.

Here is how I updated the DataGrid
emp1.name="UpdatedJohn"
myDataArray.itemUpdated(emp1) //And that will do the trick


Done!! Hurray!!

Tuesday, May 4, 2010

Flex Actionscript Goodness :Iterating Over Dictionary Object

import flash.utils.Dictionary

//Dictionary object is like key-value pair in Java, following is a code to iterate over //it

var myKeyValObj:Dictionary = new Dictionary()
myKeyValObj.firstName = "myfirstname"
myKeyValObj.lastName = "mylastname"

for (var name:String in myKeyValObj) {
trace ("myKeyValObj." + name + " = " + myKeyValObj[name]);
}

Done!! Hurray!!

Friday, April 30, 2010

Flex Actionscript Goodness: A better for loop

Along with traiditional for loop for iterating, Actionscript also allows an easier way..
following example demostrates it

var arr:Array = ["mytest1", "mytest2", "mytest3"];
// Use a regular for in loop to access the properties in arr
for ( var i in arr ) {
trace( arr[i]);
}

Done!! Hurray!!