Flex 4 two-way data binding to inconsistent type properties

Is there an easy way to do two-way data binding to properties of inconsistent types? In the example below, I tried to associate two properties with each other: one of type String ( text property from s:TextInput ), and the other of type Number ( bar property from Foo )

 package com.example { public class Foo { [Bindable] public var bar:Number; } } <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:ex="com.example.*" > <fx:Declarations> <ex:Foo id="foo" /> </fx:Declarations> <s:TextInput text="@{foo.bar}" /><!-- error at this line --> </s:Application> 

Attempting to compile this code results in the following error:

1067: Implicitly coercing a String value to an unrelated Number type.

I understand why the error occurred, but I was wondering if I did not know something (maybe some Flex 4 metadata) that would allow me to try to convert between the two types and throw an error at runtime if such a conversion fails ...

+4
source share
2 answers

I ended up using a data mapper for my object, which is blind to the data type. So, I think the only solution is to toss Object or * objects and call methods that you “know”. Although this can lead to runtime errors that were usually detected at compile time, I don't see a better solution.

0
source

The only thing I could think of was to change the type of the bar property in Foo to * (the star is a wild card that prevents type checking at compile time). But I still would like to know if there is a way to save the type ...

0
source

All Articles