Duplicate complex MXML bindings in ActionScript

MXML allows for really very powerful data binding, for example:

<mx:Button id="myBtn" label="Buy an {itemName}" visible="{itemName!=null}"/> 

I found that the BindingUtils class can bind values ​​to simple properties, but none of the bindings above do this. Is it possible to do the same in AS3 code, or does Flex automatically generate many lines of code from my MXML? Can anyone duplicate the above in pure AS3, starting with:

 var myBtn:Button = new Button(); myBtn.id="myBtn"; ??? 
+4
source share
3 answers

The way to do this is to use bindSetter . This is also the way it is done behind the scenes when MXML in your example is converted to ActionScript before compilation.

 // assuming the itemName property is defined on this: BindingUtils.bindSetter(itemNameChanged, this, ["itemName"]); // ... private function itemNameChanged( newValue : String ) : void { myBtn.label = newValue; myBtn.visible = newValue != null; } 

... except that the code generated by converting MXML to ActionScript is longer since it needs to be more general. In this example, he most likely generated two functions: one for each binding expression.

+2
source

You can also see the automatically generated code that flex does when it compiles your mxml file by adding the -keep argument to your compiler settings. You can find your settings by selecting the properties of your projects and looking at the “Flex Compiler” option, and then in the section “Additional compiler arguments:” add “-keep” to what already exists.

After execution, Flex will create a “generated” directory in the source folder, and inside you will find all the temporary files that were used during compilation.

+2
source

I believe flex generates a little anonymous function to handle this.

You can do this with ChangeWatcher. Perhaps you can even make a new anonymous function in the changewatcher call.

0
source

Source: https://habr.com/ru/post/1276929/


All Articles