Wednesday, May 5, 2010

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!!

Friday, March 19, 2010

Convert Date to String in groovy

import java.text.SimpleDateFormat

long lTime = new Date().time

String formattedDate = getUTCDateString(lTime)
println formattedDate

def getUTCDateString(long lTime)
{
Date lDate = new Date(lTime)
def cal = new GregorianCalendar(TimeZone.getTimeZone('UTC'))
cal.set(lDate[Calendar.YEAR],lDate[Calendar.MONTH],lDate[Calendar.DATE],lDate[Calendar.HOUR_OF_DAY],lDate[Calendar.MINUTE],lDate[Calendar.SECOND])
String formattedDate = new SimpleDateFormat("yyyy-mm-dd hh:mm").format(cal.time);
return formattedDate
}

Tuesday, March 9, 2010

Check if String is Number

String nbrStr = "30239023902390239023902323"
boolean b = nbrStr.isNumber()
assert b == true

Groovy String to Integer

Converting a String to Number

String nbrStr = "100"
int i = nbrStr.toInteger()
assert i==100

Sunday, December 6, 2009

Converting String to Date in Groovy

Recently I was looking for a simple way to convert a String object to a Date format in groovy..
below is the example.

In groovy, its just a one liner to make this happen

String timeStampStr = "2009-12-01T12:36:22Z"
def strDate = new Date().parse("yyyy-MM-dd'T'HH:mm:ss'Z'",timeStampStr)
println strDate.