code_junkie

Can I bind a Flex component property to a function?

Nov 13th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. private function isUserAllowed (userName:Boolean):Boolean {
  2. if (userName == 'Tom')
  3. return true;
  4. if (userName == 'Bill')
  5. return false;
  6. }
  7.  
  8. <mx:Button label="Create PO" id="createPOButton"
  9. enabled="<I want to call isUserAllowed ('Bill') or isUserAllowed ('Tom') here>"
  10. click="createPOButton_Click()" />
  11.  
  12. <mx:Button
  13. enabled = "{this.myFunction(this.myVariable)}"
  14. >
  15.  
  16. <mx:Button
  17. enabled = "{(function(arg:Object):Boolean{ ... })(this.myVariable)}"
  18. >
  19.  
  20. <?xml version="1.0" encoding="utf-8"?>
  21. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
  22.  
  23. <mx:Script>
  24. <![CDATA[
  25.  
  26. [Bindable]
  27. private var currentUser:String = "Bill";
  28.  
  29. private function isUserAllowed(user:String):Boolean
  30. {
  31. if (user == "Bill")
  32. {
  33. return true;
  34. }
  35.  
  36. return false;
  37. }
  38.  
  39. ]]>
  40. </mx:Script>
  41.  
  42. <mx:VBox>
  43. <mx:Button label="My Button" enabled="{isUserAllowed(currentUser)}" />
  44. <mx:HBox>
  45. <mx:Button label="Try Tom" click="{currentUser = 'Tom'}" />
  46. <mx:Button label="Try Bill" click="{currentUser = 'Bill'}" />
  47. </mx:HBox>
  48. </mx:VBox>
  49.  
  50. </mx:Application>
  51.  
  52. <?xml version="1.0" encoding="utf-8"?>
  53. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
  54.  
  55. <mx:Script>
  56. <![CDATA[
  57.  
  58. private var _currentUser:String = "Bill";
  59.  
  60. public function set currentUser(value:String):void
  61. {
  62. if (_currentUser != value)
  63. {
  64. _currentUser = value;
  65. dispatchEvent(new Event("userChanged"));
  66. }
  67. }
  68.  
  69. [Bindable(event="userChanged")]
  70. private function isUserEnabled():Boolean
  71. {
  72. if (_currentUser == "Bill")
  73. {
  74. return true;
  75. }
  76.  
  77. return false;
  78. }
  79.  
  80. ]]>
  81. </mx:Script>
  82.  
  83. <mx:VBox>
  84. <mx:Button label="My Button" enabled="{isUserEnabled()}" />
  85. <mx:HBox>
  86. <mx:Button label="Try Tom" click="{currentUser = 'Tom'}" />
  87. <mx:Button label="Try Bill" click="{currentUser = 'Bill'}" />
  88. </mx:HBox>
  89. </mx:VBox>
  90.  
  91. </mx:Application>
  92.  
  93. <mx:Script>
  94. <![CDATA[
  95.  
  96. [Bindable] var _username : String;
  97.  
  98. private function isUserAllowed (userName:Boolean):Boolean {
  99. if (userName == 'Tom')
  100. return true;
  101. if (userName == 'Bill')
  102. return false;
  103. }
  104.  
  105. ]]>
  106. </mx:Script>
  107.  
  108. <mx:Button label="Create PO"
  109. id="createPOButton"
  110. enabled="{isUserAllowed(_username)}"
  111. click="createPOButton_Click()" />
  112.  
  113. <mx:Script>
  114. <![CDATA[
  115. [Bindable]
  116. private var userName : String = "Tom"; // or whatever ...
  117. [Bindable]
  118. private var userAllowed : Boolean;
  119.  
  120. private function isUserAllowed ():Boolean {
  121. if (userName == 'Tom')
  122. return false;
  123. if (userName == 'Bill')
  124. return false;
  125. return false;
  126. }
  127. ]]>
  128. </mx:Script>
  129.  
  130. <mx:Button label="Create PO" id="createPOButton" enabled="{userAllowed}" addedToStage="isUserAllowed()"/>
Add Comment
Please, Sign In to add comment