Showing posts with label Flex4. Show all posts
Showing posts with label Flex4. Show all posts

Thursday, June 10, 2010

Flex GoodNess : How to bind TextInput data to a VO behind

Recently , I was looking for a way to update the VO(Value Object) when the TextInput value changes.

With Flex4, We can use Two way binding (usage: @{ } )

With Flex3, We can use the ValueCommit event to update the backend VO.

Value commit event gets fired when the TextInput control value changes through UI interaction or programmitically.

   1: //if we have a datagrid name dg, then a two way binding will look like this, Note: @ symbol is the main one
   2: <s:TextInput text="@{dg.selectedItem.name}"/> 
   3:  
   4: //Also if its Flex 3 ,where two way binding is not available, we can use the value commit event, to update the value ourself
5: <mx:TextInput id="txInput2" text="{dg.selectedItem.email}" valueCommit="txInput2_valueCommitHandler(event)"/>



Enjoy FLexing!!

Friday, June 4, 2010

Flex Goodness : NaN and Number

In flex, instead of using primitive datatypes like ent it is recommented Number datatype which supports double precision point floating number.

When a Number is not initialized, its value is NaN (Not a Number). Following are some methods which can be used to explore more about Number.

   1: var num:Number //this assigns a NaN value to it
   2: trace isNaN(num) //prints true
   3: trace int(num) //converts number to int and prints 0
   4: trace uint(num) //coverts number to uint   5:  
   6: var iStr:String = "1000"
   7: var convNum:Number = parseInt(iStr) //converts String to Number
   8: var convNumFloat:Number = parseFloat(iStr) //converts String to a float number

Enjoy Flexing!!

Thursday, June 3, 2010

Flex Actionscript Goodness : Finding and Removing a Object from ArrayCollection(IViewCursor and Sort)

There is always a need to find a particular object or remove it from the ArrayCollection class based on a various member variable conditions.

IViewCursor and Sort classes of mx.collections are a perfect fit for it

Goal: Find employee by empId and remove it

Step1 : Lets Create a ArrayCollection of two Employees

   1: public class EmployeeVo{
   2:     var empNo:String
   3:     var firstName:String
   4:     var lastName:String
   5: }
   6:  
   7: var empVo1:EmployeeVo = new EmployeeVo()
   8: empVo1.empNo=1
   9: empVo1.firstName="john"
  10: empVo1.lastName="tyson"
  11:  
  12: var empVo2:EmployeeVo = new EmployeeVo()
  13: empVo2.empNo=2
  14: empVo2.firstName="ram"
  15: empVo2.lastName="balram"
  16:  
  17: var empCol:ArrayCollection = new ArrayCollection()
  18: empCol.addItem(empVo1)
  19: empCol.addItem(empVo2)
  20:  
  21:  
  22:  
  23:  

Step2 : Sort the employee Vo Class by empId And Find it / Remove it

Note: Sort it needed first in order to use the IViewCursor


   1: import mx.collections.IViewCursor
   2: import mx.collections.Sort
   3: pubic function findAndDeleteEmp(empId:String):void{    
   4:    addSort()
   5:    var cursor:IViewCursor = empCol.createCursor()
   6:    var searchEmpVo:EmployeeVo = new EmployeeVo()
   7:    searchEmpVo.empId = empId
   8:    var isExisting:Boolean = cursor.findAny(searchEmpVo)
   9:    if(isExisting){
  10:        //to get the found object
  11:        var foundEmpVo:EmployeeVo = cursor.currentItem as EmployeeVo
  12:        //to remove the found object
  13:        var deletedObj:EmployeeVo =cursor.remove() as EmployeeVo
  14:    }
  15: }
  16:  
  17: public function addSort():void{
  18:    //Add Sorting to Array Collection
  19:     var fieldName:String = "empId"
  20:     var sortField:SortField = new SortField(fieldName, true,descending);
  21:     var sort:Sort = new Sort();
  22:     sort.fields = new Array(sortField)    
  23:     empCol.sort = sort
  24:     empCol.refresh()
  25: }

Step3: Done!! Hurray!!!

Thursday, May 27, 2010

Flex Actionscript GoodNess: AS equivalent of Java Hashmap / Hashtable

With Java being the my core language, it always puzzles me why is there is not good Hashtable functionality..

There are lot of equivalents though, but I still miss the Java Hashtable.

Here are the Options

1. Associative Arrays

   1: var params:Array = new Array()
   2: params.firstName = txName.text
   3: params.lastName = txLName.text
   4: displayValues(params)
   5:  
   6: public static const FIRST_NAME:String ="firstName"
   7: public static const LAST_NAME:String = "lastName"
   8: //Retreive values From Associative Array
   9: public function displayValues(params:Array):void{
  10:     var firstName:String = params[FIRST_NAME]
  11:     var lastName:String = params[LAST_NAME]
  12:     trace(firstName)
  13:     trace(lastName)
  14: }





2. flash.utils.Dictionary



   1: import flash.utils.Dictionary
   2:  
   3: //Set values to dictionary
   4: public function dictionaryDemo(fName:String, lName:String):void{
   5:     var aDict:Dictionary = new Dictionary()
   6:     aDict.firstName = fName
   7:     aDict.lastName = lName
   8:      displayValues(aDict)
   9: }
  10: public function displayValues(aDict:Dictionary):void{
  11:     trace(aDict.firstName)
  12:     trace(aDict.lastName)
  13:  
  14: //You can also iterate over a Dictionary
  15:     for (val in aDict) { 
  16:         trace ("KeyVal pair is " + val + " = " + aDict[val]); 
  17:     }
  18: }
  19:  


What do you guys think, which one is better Associative Arrays or Dictionary..



By default, I have observed that actionscript Object type behaves like dictionary.

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