2009-01-15 68 views
7

我想要設置啓用基於具有一個或多個參數的函數的返回值的按鈕上的屬性。我怎樣才能做到這一點?我可以將Flex組件屬性綁定到一個函數嗎?

private function isUserAllowed (userName:Boolean):Boolean { 
    if (userName == 'Tom') 
     return true; 
    if (userName == 'Bill') 
     return false; 
} 

<mx:Button label="Create PO" id="createPOButton" 
enabled="<I want to call isUserAllowed ('Bill') or isUserAllowed ('Tom') here>" 
click="createPOButton_Click()" /> 

回答

7
<mx:Button 
enabled = "{this.myFunction(this.myVariable)}" 
> 

或內聯:

<mx:Button 
enabled = "{(function(arg:Object):Boolean{ ... })(this.myVariable)}" 
> 
0
<mx:Script> 
    <![CDATA[ 
     [Bindable] 
     private var userName : String = "Tom"; // or whatever ... 
     [Bindable] 
     private var userAllowed : Boolean; 

     private function isUserAllowed():Boolean { 
      if (userName == 'Tom') 
      return false; 
      if (userName == 'Bill') 
       return false; 
      return false; 
     } 
    ]]> 
</mx:Script> 

    <mx:Button label="Create PO" id="createPOButton" enabled="{userAllowed}" addedToStage="isUserAllowed()"/> 
+0

但是,在我的參數傳遞給函數isUserAllowed?我想在試圖設置enabled屬性時動態地調用該函數。 – SkunkSpinner 2009-01-15 01:50:34

+0

試試這個,我更新了它。 – ForYourOwnGood 2009-01-15 01:54:40

+0

該變量將成爲全局變量,不需要將其傳遞給該函數。 – ForYourOwnGood 2009-01-15 19:39:47

2

下面是我在類似的情況下做了幾次:

<mx:Script> 
<![CDATA[ 

    [Bindable] var _username : String; 

    private function isUserAllowed (userName:Boolean):Boolean { 
     if (userName == 'Tom') 
      return true; 
     if (userName == 'Bill') 
      return false; 
    } 

]]> 
</mx:Script> 

<mx:Button label="Create PO" 
    id="createPOButton" 
    enabled="{isUserAllowed(_username)}" 
    click="createPOButton_Click()" /> 

這樣,當可綁定_username的變化,它會觸發一個變更通知。由於標籤正在監聽_username更改(即使它只是另一個函數的參數),因此將重新評估enabled屬性。

10

According to the Flex docs,只要屬性是可綁定的,你可以簡單地做到這一點(我已經包含了兩個額外的按鈕來演示):

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 

    <mx:Script> 
     <![CDATA[ 

      [Bindable] 
      private var currentUser:String = "Bill"; 

      private function isUserAllowed(user:String):Boolean 
      { 
       if (user == "Bill") 
       { 
        return true; 
       } 

       return false; 
      } 

     ]]> 
    </mx:Script> 

    <mx:VBox> 
     <mx:Button label="My Button" enabled="{isUserAllowed(currentUser)}" /> 
     <mx:HBox> 
      <mx:Button label="Try Tom" click="{currentUser = 'Tom'}" /> 
      <mx:Button label="Try Bill" click="{currentUser = 'Bill'}" /> 
     </mx:HBox> 
    </mx:VBox> 

</mx:Application> 

沒有currentUser標記爲[綁定],雖然,它贏得沒有工作。另一種方法,如果你想更直接地綁定到函數(這也表示在文檔中),可以讓函數響應當前用戶發生更改時發送的事件,如下所示:

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()"> 

    <mx:Script> 
     <![CDATA[ 

      private var _currentUser:String = "Bill"; 

      public function set currentUser(value:String):void 
      { 
       if (_currentUser != value) 
       { 
        _currentUser = value; 
        dispatchEvent(new Event("userChanged")); 
       } 
      }   

      [Bindable(event="userChanged")] 
      private function isUserEnabled():Boolean 
      { 
       if (_currentUser == "Bill") 
       { 
        return true; 
       } 

       return false; 
      } 

     ]]> 
    </mx:Script> 

    <mx:VBox> 
     <mx:Button label="My Button" enabled="{isUserEnabled()}" /> 
     <mx:HBox> 
      <mx:Button label="Try Tom" click="{currentUser = 'Tom'}" /> 
      <mx:Button label="Try Bill" click="{currentUser = 'Bill'}" /> 
     </mx:HBox> 
    </mx:VBox> 

</mx:Application> 

所以有幾種方法。國際海事組織,第二個看起來似乎更合適,但第一個沒有錯。祝你好運!

相關問題