I have the following code:
<fx:Script>
<![CDATA[
import shared.GlobalsManager;
import app.Globals;
protected var globals:Globals=GlobalsManager.getGlobals();
]]>
</fx:Script>
<s:FormItem label="Server or Client:">
<s:DropDownList id="serverOrClient" dataProvider="{globals.serverOrClientOptions}" />
</s:FormItem>
And the relevant Globals class code (Globals extends from GlobalsAdminGlobalS)
public class GlobalsAdminGlobalS extends GlobalsAdminS {
public static const ServerOrClient_server:String="server";
public static const ServerOrClient_client:String="client";
public static const ServerOrClient_both:String="both";
[Bindable]
public var serverOrClientOptions:ArrayCollection=new ArrayCollection(
[ServerOrClient_server,ServerOrClient_client,ServerOrClient_both]);
[Bindable]
public var appOrAdminOptions:ArrayCollection=new ArrayCollection(
[AppOrAdmin_App,AppOrAdmin_Admin]);
}
I am getting the following warning with the Flex 4.5.1 compiler:
WARNING S:\_flash\shared\shared\src\shared\admin\global\ClassManager.mxml[28]:
Data binding will not be able to detect assignments to "globals".
<s:DropDownList id="serverOrClient" dataProvider="{globals.serverOrClientOptions}" />
I have not tested if it actually binds. I dont see what the problem is. According to this and what the expected functionality is, one does not need to mark an entire class bindable:
http://www.adobe.com/devnet/flex/articles/databinding_pitfalls.html
EDIT: The Script block is located in a parent of the of the FormItem. The global variable is marked as protected.
1) Can children of a component access properties defined in the component’s script blocks?
2) If the functionality of properties defined in an ancestor’s script blocks, is the same as properties of a class instance (ie they are not magically available in the curly braces of child components), does a descendant generally access the ancestor or any another component instance through its id property?
To fix the warning, you need to make the protected
globalsvariable bindable in your first code snippet:Yes, you do not need to make the entire GlobalsManager class bindable. But, in your first code snippet, you are using the
globalsvariable inside of a curly brace expression (in the assignment for the dataProvider).Any variable used inside of a curly brace expression must be bindable…
[Edit]
Note, if a variable or property used in a curly brace expression is NOT bindable, the curly brace expression gets evaluated once and only once. When the vars/props in the curly brace epxression are bindable, they get evaluated each time the var/property changes.
[Additional Answers]
If you declare variable as
publicin the script block, yes, it will be accessible.Yes, the id property in mxml is a variable name that you can refer to the object in Actionscript statements.