views:

336

answers:

4

I'm re-writing an MXML item renderer in pure AS. A problem I can't seem to get past is how to have each item renderer react to a change on a static property on the item renderer class. In the MXML version, I have the following binding set up on the item renderer:

instanceProperty={callInstanceFunction(ItemRenderer.staticProperty)}

What would be the equivalent way of setting this up in AS (using BindingUtils, I assume)?

UPDATE:
So I thought the following wasn't working, but it appears as if Flex is suppressing errors thrown in the instanceFunction, making it appear as if the binding itself is bad.

BindingUtils.bindSetter(instanceFunction, ItemRenderer, "staticProperty");

However, when instanceFunction is called, already initialized variables on the given instance are all null, which was the cause of the errors referenced above. Any ideas why this is?

A: 

I think you need to respond to the "PropertyChanged" event.

CookieOfFortune
Can you please elaborate a little? How/when is this event dispatched?
Stiggler
A: 

If you're going to do that, use a singleton instead of static. I don't think it will work on a static. (If you have to do it that way at all, there are probably a couple ways you could reapproach this that would be better).

var instance:ItemRenderer = ItemRenderer.getInstance();
BindingUtils.bindProperty(this, "myProperty", instance, "theirProperty");
Sean Clark Hess
Sorry, but I'm not looking for a different approach, because this one appears to work in MXML. I'm wondering about the work that goes on behind the scenes.
Stiggler
Well, binding sets up event listeners for the properties. You can't listen to a class object, because it isn't an EventDispatcher. You could do it if your class extended EventDispatcher and you went with the the singleton method. As bad as singleton's can be, Static classes are the worst. It might work in MXML, but it almost definitely isn't bound. It might just find the value once and never update after that.
Sean Clark Hess
I appreciate your time, but it seems we're not on the same page. First of all, I'm looking to do this for an ItemRenderer, implying that the class will have multiple instances, immediately ruling out any use of a singleton, for better or worse. Secondly, binding to static properties is allowed, as long as it isn't a static getter/setter. The binding I've set up in MXML works not just once, but 100% of the time.
Stiggler
A: 

After fiddling with this for a while, I have concluded that this currently isn't possible in ActionScript, not even with bindSetter. It seems there are some MXML-only features of data bindings judging by the following excerpt from the Adobe docs (though isn't it all compiled to AS code anyways)?

You cannot include functions or array elements in property chains in a data binding expression defined by the bindProperty() or bindSetter() method. For more information on property chains, see Working with bindable property chains.

Source: http://livedocs.adobe.com/flex/3/html/help.html?content=databinding_7.html

Stiggler
A: 

You can create a HostProxy class to stand in for the funciton call. Sort of like a HostFunctionProxy class which extends from proxy, and has a getProperty("functionInvokeStringWithParameters") which will invoke the function remotely from the host, and dispatch a "change" event to trigger the binding in typical [Bindable("change")] Proxy class. You than let the HostProxy class act as the host, and use the property to remotely trigger the function call. Of course, it'd be cooler to have some TypeHelperUtil to allow converting raw string values to serialized type values at runtime for method parameters (splitted by commas usually).

Example: eg. var standInHost:Object = new HostFunctionProxy(someModelClassWithMethod, "theMethodToCall(20,11)"); // With BindingUtils..... // bind host: standInHost // bind property: "theMethodToCall(20,11)"

Of course, you nee to create such a utlity to help support such functionality beyond the basic Flex prescription. It seems many of such (more advanced) Flex bindings are usually done at compile time, but now you have to create code to do this at runtime in a completely cross-platform Actionscript manner without relying on the Flex framework.

Glenn