1
votes

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
Joshua, I hope this is what you want, more or less: stackoverflow.com/questions/3081229/… - amn

3 Answers

3
votes

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

1
votes
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.

0
votes

Disclosure: shameless self promotion

The BindageTools library provides an intuitive builder API for setting up bindings in ActionScript.