2010-02-01 56 views
1

我是一個完整的Flex/Flash noob,運行Adobe Flash Builder 4 Beta 2.我有一個主要組件,需要能夠調用幾個彈出窗口,每個彈出窗口大多都相同,除了一個函數和幾個標籤。顯然,我更喜歡能夠定義這個函數,並在調用彈出窗口時更改這些標籤,而不是使用幾乎相同的代碼來創建大量的.mxml文件,我只是不知道該怎麼做。我想出瞭如何改變標籤,但不知道如何重新定義這個功能。Flex - 如何在另一個組件中定義一個功能?

爲簡單起見,假設我的代碼如下所示:

main.mxml:

<?xml version="1.0" encoding="utf-8"?> 
    <mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009" :s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" creationComplete="init()"> 
     <fx:Script> 
      <![CDATA[ 
       import mx.controls.Alert; 
       import mx.managers.PopUpManager; 

       protected function init():void 
       { 
        var alertWindow:IFlexDisplayObject = PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject, popup, true); 
        PopUpManager.centerPopUp(alertWindow); 
        var popInstance:transmitRoundPop = alertWindow as transmitRoundPop; 
        popInstance.btnTest.label = "NEW"; 
       } 
      ]]> 
     </fx:Script> 
    </mx:Module> 

popup.mxml:

<?xml version="1.0" encoding="utf-8"?> 
    <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" width="400" height="300" xmlns:flash="services.flash.*"> 
     <fx:Script> 
      <![CDATA[ 
       import mx.controls.Alert; 
       import mx.managers.PopUpManager; 

       public function test():void 
       { 
        Alert.show("ORIGINAL"); 
        PopUpManager.removePopUp(this); 
       } 
      ]]> 
     </fx:Script> 
     <s:Panel x="10" y="10" width="380" height="280" title="Test" id="pnlTest"> 
       <s:Button x="131" y="104" label="OLD" id="btnTest" click="test()"/> 
     </s:Panel> 
    </s:Group> 

現在說我要改變test()在在main.mxml中調用它時popup.mxml ......我該怎麼做?請包括詳細信息...請記住我是一個菜鳥:-)

+0

你是什麼改變測試意味着()?改變它的代碼?參數? – CookieOfFortune 2010-02-01 17:45:30

+0

對不起,沒有想好。我想更改其中的代碼,或者更準確地覆蓋/覆蓋/重新定義該函數,但我認爲我現在已經找到了正確的方法。 – Travesty3 2010-02-01 17:59:26

回答

1

當然,張貼的想法浮現在我腦海中的問題之後。我想我會在這裏發佈解決方案,而不是僅僅刪除問題,以防其他人幫助。

我只是把test()出完全popup.mxml和main.mxml修改init()看起來像這樣:

protected function init():void 
{ 
    var alertWindow:IFlexDisplayObject = PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject, popup, true); 
    PopUpManager.centerPopUp(alertWindow); 
    var popInstance:transmitRoundPop = alertWindow as transmitRoundPop; 
    popInstance.btnTest.label = "NEW"; 
    popInstance.btnTest.addEventListener("click", function():void { Alert.show("REDEFINED"); }); 
} 
0

如果我得到你問的問題,你可以使測試()一個函數變量,並給它的公共訪問,以便其他組件可以改變它。

<fx:Script> 
     <![CDATA[ 
      import mx.controls.Alert; 
      import mx.managers.PopUpManager; 

      // "test" is now a function variable, 
      // you change it just like any other variable, 
      // but you can call it as well, just like before. 
      public var test:Function; 

      public function defaultTest():void 
      { 
       Alert.show("ORIGINAL"); 
       PopUpManager.removePopUp(this); 
      } 

      protected function init():void 
      { 
      // Setting a default value for test 
      // otherwise it would give you an error when calling 
      // an unassigned function variable. 
       this.test = defaultTest; 
       ... 
      } 
     ]]> 
    </fx:Script> 

現在,在另一個組件:

public function mainTest():void 
{ 
    ... 
} 

... 
myPopup.test = mainTest; // setting the variable, note that there are no parentheses. 
myPopup.test(); // running the function, note that there are now parentheses. 
+0

我也試過這個,但它並不適合我。當用戶點擊彈出窗口中的按鈕時會調用該函數,因此該函數需要從popup.mxml中調用,而不是main.mxml。即使在執行myPopup.test = mainTest之後,它仍在調用defaultTest()。我懷疑(可能是錯誤的),這是因爲彈出窗口已經在那個時候創建​​了。在創建彈出窗口之前,我無法更改該功能,因爲我需要彈出窗口來執行此操作。然後再次也有可能我只是沒有做正確的事情......你的方式對我也很有意義。 – Travesty3 2010-02-01 18:20:36

+0

是的,我相信PopupManager會創建自己的彈出窗口。我相信你上面的代碼是alertWindow的一部分。 – CookieOfFortune 2010-02-01 18:29:49

相關問題