Flex: Binding to member variables

19 02 2008

I’ve just spent the past few hours wondering why some charts weren’t updating when a new data set loaded. I had the following code in my class:

BindingUtils.bindProperty(columnChart, "dataProvider", dataObj, ["paramA", "paramB"]);

dataObj is a variable Binded to the model, in this case the model is an ArrayCollection of chart data. When the model changed, dataObj changed and was updating correctly but for some reason columnChart wasn’t picking up this change even though I thought I had binded it to dataObj. Eventually I tried the following line of code instead:

BindingUtils.bindProperty(columnChart, "dataProvider", this, ["dataObj", "paramA", "paramB"]);

Instead of trying to bind to dataObj I bind to this and pass dataObj as the first item in the array for the chain param. This worked. The chart updated when the model changed. This is clearly something to do with the way Flex constructs the chain. The key to remember here is when binding to a member variable you must bind to this and specifiy the variable as the host, either as a String or as the 1st String in an Array.