Tuesday, December 2, 2008

mx.controls.List and empty dataProviders

If you bind a List's dataProvider to an ArrayCollection that happens to be empty at the time of binding, the List will ignore it and use its own ArrayCollection as its dataProvider. Later, when you populate your ArrayCollection, the List won't show the changes since it isn't using it.

Yes, really.

I have submitted this to Adobe as http://bugs.adobe.com/jira/browse/SDK-18288.

There are two workarounds:

1. Don't bind to an empty ArrayCollection. Pre-initialize the ArrayCollection to something non-empty. For example:
[Bindable]
public var results : ArrayCollection = new ArrayCollection([ "" ]);

2. Don't bind until after you populate the ArrayCollection with real data:
public function populateMyCollection() : void
{
results.addItem("Hello");
results.addItem("World");

myList.dataProvider = results;
}

Tuesday, November 4, 2008

FlexBuilder and XML

This is legal Flex syntax:

function foo() : void
{
var bar : XML = <mytag attr1="1"/>;
}

function bar() : void
{
}

However, the fact that your XML variable has an implicit closing tag instead of an explicit one will confuse poor FlexBuilder's brain, so that it doesn't detect bar (or, in fact, anything after foo) as a function. Symptoms include:

  1. Functions after foo won't show up in the Outline View

  2. FlexBuilder won't let you use F3 ("Goto Definition") to go to anything in that class


The solution is to put in a closing tag:
function foo() : void
{
var bar : XML = <mytag attr1="1"></mytag>;
}

function bar() : void
{
}

Flex VerifyError #1031: Scope depth is unbalanced

Flex doesn't like returns from blocks nested in switch/case statements. For example, if you do this:

public function foo(thing : String, cond : Boolean) : void
{
switch (thing)
{
case "foo":
if (cond)
{
return "Hello";
}
return "World";
case "bar":
// stuff
}
}


You'll get a VerifyError if you hit the return "Hello" line.

The solution is to set a return variable and do your return from the case, rather than from the if:

    case "foo":
var ret : String;
if (cond)
{
ret = "Hello";
}
else
{
ret = "World";
}
return ret;

Friday, October 17, 2008

ActionScript scoping weirdness

Today I got the following error when I ran my Flex app:


TypeError: Error #1010: A term is undefined and has no properties.


Some quick searching on the web didn't reveal much except that it might have to do with a DisplayObject having visible == false. Great, except the object in question isn't a DisplayObject.

I looked into the code and saw this:

private function foo() : void
{
myObject.myProp = "Hello World";

var myObj : MyClass = new MyClass();

// stuff
}

The problem is that I was accidentally using myObject before actually creating it. In any other language, this would generate a compile error, but ActionScript, in its infinite wisdom, scopes the variable to the entire function, not just to the code following the declaration.

The solution: Move the offending line to after the declaration.

Thursday, October 16, 2008

Forcing screen updates in Flex/ActionScript 3

I'm working on a Flex app that has full undo functionality. After an undo or redo, sometime the display is out of sync with the underlying data. That's because various things don't refresh themselves properly even with data binding.

For example, if you have a DataGrid backed by an ArrayCollection, changes to the collection will reflect properly in the display because the DataGrid is listening for collection events. But if you changed one property of an object in the collection, DataGrid may not pick that up (see http://www.barneyb.com/barneyblog/2007/06/23/another-flex-weirdnessgotcha/)

I experimented with the ideas presented in that article and found that calling executeBindings(true) on my app's root object after an undo works beautifully:

public function postUndo() : void
{
Application.application.executeBindings(true);
}

Upon further reflection and experimentation, it looks like the reason DataGrid might not see property changes has to do with a subtlety in data binding. If your DataGridColumn's dataField is marked as [Bindable], then changes should come through OK. For example, you might have this:

public class MyObject
{
private var m_foo : String;

[Bindable]
public function get Foo() : String
{
return m_foo;
}

public function set Foo(foo : String) : void
{
m_foo = foo;
}
}

...

<mx:datagrid dataprovider="some ArrayCollection of MyObject">
<mx:datagridcolumn datafield="Foo">
</mx:datagridcolumn>
</mx:datagrid>

In this case, any time you set someMyObject.Foo, the grid will update.

But because data binding wraps the setter in a proxy to cause events to fire, if you set m_foo directly, the data binding events never fire, and the grid datagrid doesn't update. That's when you'd need to call executeBindings().