Comparing two custom ActionScript classes

I have a custom employee class in my Flex that has name, badge number, phone number, etc, etc. In the form to update it, I want to only actually do to the database if the record was changed. The save function takes the existing record and the new record with changes and shoves it to ColdFusion, who generates an email with the before and after states.

I could have disabled the "save" button if the record isn't changed, only giving the user a "close" button. But there are a lot of fields on the form and more are being added in the future. I don't want some giant function that determines if the before and after employee objects are different, comparing one attribute at a time.

So I went to the kitchen cabinet and got some serial.

[More]

Flex selectComboBox(), mkII

At the suggestion of a coworker, I've made a tiny change to my selectComboBox() function. The purpose of this function is to automatically select something in a ComboBox, such as if you're editing a form. But what if the function ends up doing nothing because no matches were found? Now it'll return false in that case. Also I properly capitalized it.

[More]

cfabort for cfscript

There is a version of this on cflib, but I need a little more. From cfscript, you and pass a whole scope to dump it and stop page execution.

<cfscript>
   cfabort(LOCAL);
</cfscript>

<cffunction name="cfabort">
   <cfargument name="dumpme" required="false"/>
   
   <cfif structkeyexists(arguments,"dumpme")>
      <cfdump var="#arguments.dumpme#"/>
   </cfif>
   
   <cfabort/>
</cffunction>

Flex selectDatagrid()

"omg, murloc, it's a post that's not about video games!"

Problem: You programmically are selecting an item from a datagrid.

Solution: The function below! Pass the datagrid, column you're matching, and the value to match on.

private function selectDatagrid(dg:DataGrid, column:String, value:String):void{
   if( value == null )
      return;
   
   for( var i:int=0; i<dg.dataProvider.length; i++ ){
      if( value == String(dg.dataProvider[i][column]) ){
         dg.selectedIndex = i;
         dg.verticalScrollPosition = i;
         break;
      }
   }
}

Flex selectCombobox()

Problem: You programmically are selecting an item from a combobox.

Solution: The function below! Pass the combobox, column you're matching, and the value to match on.

private function selectCombobox(cb:ComboBox, column:String, value:String):void{
   if( value == null )
      return;
   
   for( var i:int=0; i<cb.dataProvider.length; i++ ){
      if( value == String(cb.dataProvider[i][column]) ){
         cb.selectedIndex = i;
         break;
      }
   }
}

Data loading, PleaseWait

Have a RemoteObject (WebService, HTTPRequest) call that takes a while to come back? Can't really do anything until it does come back? Give the user something to do! If you have a long running process and nothing for the user to see/do, they'll get distracted and move from your app to do something else.

PleaseWait.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Panel
   xmlns:mx="http://www.adobe.com/2006/mxml"
   title="Please Wait ..."
   horizontalAlign="center"
   width="285" height="90">

   
   <mx:Label
      text="Please wait while employee data is loaded"/>

   
   <mx:ProgressBar
      indeterminate="true"
      labelPlacement="center"/>

   
</mx:Panel>

[More]

Ask a Murloc: Flex email link

I was asked the Flex equivalent to <a href="mailto:..."> in Flex. Mailto is just another protocol, like http and ftp, so can be opened the same way as if you were going to load up an external site from Flex.

private function openEmail(email:String):void{
   var url:String = "mailto://" + email;
   var webPageURL:URLRequest = new URLRequest( url );
   navigateToURL(webPageURL,"_self");
}

This opens the user's default mail client. The "_self" keeps it from opening another window/tab.

Printing in Flex

I had to print something from Flex, but the LiveDocs weren't that helpful. I found this function and modified it to make it more generic...

<mx:Script>
   <![CDATA[
      import mx.core.IUIComponent;
      import mx.printing.FlexPrintJob;

      private function doPrint(printMe:IUIComponent):void{
         var pj:FlexPrintJob = new FlexPrintJob();
         if( pj.start() != true ){
            return;
         }
         pj.addObject(printMe);
         pj.send();
      }
   ]]>
</mx:Script>

Then define some display object you'll want to print. I loaded another component into this canvas.

<mx:Canvas
   id="formContent" backgroundColor="#ffffff"/>

And a button to print, passing it the component above. It's that simple!

<mx:Button label="Print" click="doPrint(formContent)"/>

No DateAdd()…?

Either I've missed something, or there is no DateAdd() function built into the basic Flex installation. Well that simply won't do, I need to be able to add dates! There's no DateDiff(), either. I must be missing something.

private function DateAdd(origDate:Date, addVal:int, addUnit:String):Date{
   var newDate:Date;

   switch(addUnit){
      case 'y':
         newDate = new Date(origDate.fullYear+addVal, origDate.month, origDate.date);
         break;
      case 'm':
         newDate = new Date(origDate.fullYear, origDate.month+addVal, origDate.date);
         break;
      case 'd':
         newDate = new Date(origDate.fullYear, origDate.month, origDate.date+addVal);
         break;
   }

   return newDate;
}

BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner