nwebb

Flex, Flash, AIR

Archive for June, 2008

Using Functions As A DataBinding Source

When the value of a bindable property changes you may want to format the data before displaying it, or even use the data to generate some new data for display.

For example, you have a Combo component which needs to display a range of years - here is an example (right-click to view source):

The data you receive may be in the following format (generic object used here for simplicity):

Actionscript:
  1. /**
  2. * A generic object representing a range of years to display.
  3. *@param startYear  The year at which to begin
  4. *@param numYears  The total number of years to display (including startYear)
  5. *E.g. intended to display the years  2001 -> 2008
  6. */
  7. {startYear:2001, numYears:8}

As you can see, it's not in a format suitable for our Combo, but this object may be what you are assigning to some bindable property:

Actionscript:
  1. [Bindable]
  2. private var dateRangeObj:Object;

It would be nice if every time it changed we could parse through it, generate an array of years to be displayed and have our Combo display that Array as its dataProvider value.

Solution
Rather than simply setting the bindable property dateRangeObj as the dataProvider for our Combo, we can also wrap it in a method call:

Actionscript:
  1. dataProvider=" { createYears( dateRangeObj ) } "

This ensures that every time the value of dateRangeObj changes, the method createYears() is triggered and the dateRangeObj is sent in as an argument. We then use the data within dateRangeObj to create and return a brand new Array, which the Combo then displays:

Actionscript:
  1. private function createYears(dateRangeObject:Object):Array
  2. {
  3.     var years:Array = [];
  4.     for(var i:uint=0; i<dateRangeObject.numYears; i++)
  5.     {
  6.         years.push(dateRangeObject.startYear + i);
  7.     }
  8.     return years;
  9. }

This is not the only way to trigger a function upon a binding value change, but it's quite a compact way of doing it.

As you can see, the only place createYears() is called is from within the binding, yet it's triggered every time dateRangeObj is updated.

2 comments

Flex – Debug Player & Firefox 3 Plugins Issue

If you're using FF3 and have noticed that your breakpoints are being ignored or that your trace statements are no longer outputting anything then check out this Jira issue: http://bugs.adobe.com/jira/browse/FB-13064

In my case the 'culprit' turned out to be the Adblock Plus plugin, although there are obviously bigger issues here.  As an interim solution I disabled the plugin and went on my merry way.

Update (28/06/2008):

This is Mozilla bug 441424
https://bugzilla.mozilla.org/show_bug.cgi?id=441424 which is a dupe of Mozilla https://bugzilla.mozilla.org/show_bug.cgi?id=438830

We are hoping it will be fixed in FF 3.01. Feel free to add votes to the Mozilla bug.
Resolving as external.

3 comments

Same DataGrid, Different States

Quick Tip:

I've just been asked how to add additional columns to a DataGrid when changing from one state to another:

Most people who create different states do so in Design View because the syntax is not particularly intuitive. It doesn't appear that you can add new columns to an existing DataGrid in Design View, and while I'm sure you can hand-code the tags, the solution can be simplified if you approach it from a different angle. Create all the necessary columns in your base state, then set the ones you don't need in the base state, to be invisible (rather than actually trying to add additional columns). In your second state you can use the SetProperty tag to toggle their 'visibile' property.

Right-click the above example to view source or check out the code below:

Actionscript:
  1. //DataGrid:
  2. <mx:DataGrid width="100%" height="100%">
  3.     <mx:columns>
  4.         <mx:DataGridColumn headerText="Name"/>
  5.         <mx:DataGridColumn id="ageCol" headerText="Age" visible="false"/>
  6.     </mx:columns>
  7. </mx:DataGrid>
  8.  
  9. //state stuff:
  10. <mx:states>
  11.     <mx:State name="AdminState">
  12.         <mx:SetProperty target="{ageCol}" name="visible" value="true"/>
  13.     </mx:State>
  14. </mx:states>

3 comments

Next Page »

Bad Behavior has blocked 399 access attempts in the last 7 days.