views:

62

answers:

2

When Flex Sees Something Like This:

<mx:Label text="Hello {MyVar} World!"/>

It Must Translate That Somehow Into ActionScript. But What If I Need To Do Something Similar, At Runtime. How Can I Accomplish What DYNAMICALLY? WHEN I DO NOT KNOW THE CONTENTS OF THE BINDING TEMPLATE.

In ActionScript it would need it to look something like this:

public function CustomDynamicBinding(StringToBind:String):Label {
  // *EXAMPLES* Of StringToBind:
  //    "Hello {MyVar} World!"
  //    "Product: {name} ${price}.00"
  //    "{data.label}, {data.description}"
  // I've Written It This Way Because I DO NOT KNOW The Exact Text To Be Bound At Design Time.
  [Bindable]
    var Lab:Label=new Label();
    Lab.text=???
    return(Lab);
}

How can I accomplish this kind of "Dynamic" binding... Where I don't know the value of "StringToBind" until runtime? For the purposes of this question we can assume that I do know that any variable(s) mentioned in "StringToBind", are guaranteed to exist at runtime.

I already realize there are much more straightforward ways to accomplish this exact thing STATICALLY, and using only Flex/MXML. It's important for my project though that I understand how this could be accomplished without MXML.

Doing This: lab.text = stringToBind.replace("{myVar}", str);

Will NOT work because this simply assigns ONCE the value of "{myVar}" - (which may not even BE the variable referenced in "stringToBind"!!) to the label, and does not take into account when and if myVar changes! Wouldn't I need to somehow use something Like bindProperty?

+3  A: 

Use BindingUtils.bindSetter

var stringToBind:String = "Hello {myVar} World!";
[Bindable]
var myVar:String = 'Flex';
var lab:Label = new Label();
BindingUtils.bindSetter(labelValue, this, "myVar");
function set labelValue(str:String):void
{
  lab.text = "Hello " + str + " World!";
  //or if you want it dynamic
  lab.text = stringToBind.replace("{myVar}", str);
}

Note that this is not pure ActionScript in its strict sense as data binding itself is a Flex concept; this is just MXML-less syntax of doing it. You're still using Flex binding internally - but again, use of Label alone makes if Flexy

Amarghosh
You have hard coded the "StringToBind". The whole POINT is that what's being bound is not know at design time.
Joshua
The label text will also not change when myVar changes, if done this way.
Joshua
@Joshua Did you even try this? Label text will change when `myVar` changes. And about hard coding - this is not as hard coded as `<mx:Label text="Hello {MyVar} World!"/>` is hard coded. You can always update `stringToBind` at runtime and use my second method.
Amarghosh
@Joshua And btw, did you down vote this?
Amarghosh
@Joshua You're missing the point. The point was to use bindSetter to set up a function that will execute when the value of a property of your choosing changes. Whatever you do in that function is up to you.
bug-a-lot
+1  A: 
private function _BindingSource_bindingsSetup():Array
{
    var result:Array = [];

    result[0] = new mx.binding.Binding(this,
        function():String
        {
            var result:* = "Hello " + (MyVar) + " World!";
            return (result == undefined ? null : String(result));
        },
        null,
        "_BindingSource_Label1.text"
        );


    return result;
}

It's only a part of generated code. Feel free to add -keep-generated-actionscript parameter to compiler options and read all generated ActionScript in bin-debug\generated.

Maxim Kachurovskiy
You have hard-coded "Hello World" and "MyVar"... How does this help me?
Joshua