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="@{}"/> 
   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="{}" 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: }
   7: var empVo1:EmployeeVo = new EmployeeVo()
   8: empVo1.empNo=1
   9: empVo1.firstName="john"
  10: empVo1.lastName="tyson"
  12: var empVo2:EmployeeVo = new EmployeeVo()
  13: empVo2.empNo=2
  14: empVo2.firstName="ram"
  15: empVo2.lastName="balram"
  17: var empCol:ArrayCollection = new ArrayCollection()
  18: empCol.addItem(empVo1)
  19: empCol.addItem(empVo2)

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