2012-07-22 3 views
0

변수가 초기화되었는지 여부를 감지하는 데 문제가있는 Flex 응용 프로그램을 구축하고 있습니다. this.pageObject 항상 몇 번 constructMenu() 메소드를 호출하거나하는 횟수 FoodMenu()을에 관계없이 NOT NULL로 등록하는 이유는 누군가가 설명해주십시오 수플래시 인스턴스 변수 항상 null입니까?

<fx:Script> 
    <![CDATA[ 
    private var pageObject:* = null; //Yes the "*" data type is needed ;) 

    //Fired by the application elseware 
    private function constructMenu(e:ResultEvent):void { 
     if (this.pageObject != null) { 
     //This block never runs... 

     // The pageObject will be null when the application 
     // first runs, so skip this first time around. 
     // However... this method will be called multiple 
     // times. Since FoodMenu() is a display object 
     // and is instantiated below, all subsequent calls 
     // to this method will require us to remove the old 
     // display object before adding a new one. 
     } else { 
     // This block always runs... 
     } 

     setTimeout(function():void { 
     this.pageObject = new FoodMenu(); //Should now be not-null!!! 

     //Do more stuff with the FoodMenu() and add to the main application 
     }, 1000); 
    } 
    ]]> 
</fx:Script> 

주어진 코드 샘플 및 댓글 :이 간단한 코드 샘플을 관찰 클래스가 인스턴스화 되었습니까?

감사합니다.

편집 :

setTimeout(function():void { 
    this.pageObject = new FoodMenu(); //Should now be not-null!!! 
    //Do more stuff with the FoodMenu() and add to the main application 
}, 1000); 

은 "이"포인터가 뭔가 다른 의미 (아마도 글로벌 : 요청

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" 
      xmlns:components="components.*" 
      xmlns:pagesservice="services.pagesservice.*" 
      xmlns:menuservice="services.menuservice.*" 
      minWidth="955" minHeight="600" backgroundColor="0x141414" 
      creationComplete="init(event)" skinClass="skins.Theme" xmlns:pages="components.pages.*" xmlns:reviewsservice="services.reviewsservice.*"> 
<fx:Script> 
    <![CDATA[ 
     import com.asual.swfaddress.*; 
     import com.forwardfour.boncuisson.events.MenuEvent; 
     import com.greensock.TweenMax; 
     import com.greensock.plugins.VisiblePlugin; 

     import components.header.Menu; 
     import components.pages.FoodMenu; 

     import flash.utils.setTimeout; 

     import mx.controls.Alert; 
     import mx.core.IVisualElement; 
     import mx.events.FlexEvent; 
     import mx.rpc.events.FaultEvent; 
     import mx.rpc.events.ResultEvent; 

     import skins.Theme; 

     import spark.components.Group; 

    //Globalize a reference to the navigation menu 
     private var menu:Menu; 

    //Globalize a reference to the page content 
     private var pageObject:* = null; //This reference will be various types, so just make a general object 

    /** 
    * Initialization 
    * ------------------------------- 
    */ 

    //Add an event listener for when "links" on the menu are clicked and initialize SWFAddress 
     private function init(event:FlexEvent):void { 
     //Listen for menu clicks 
      var skin:Theme = Theme(this.skin); 
      this.menu = Menu(skin.menu); 
      this.menu.addEventListener(Menu.MENU_ITEM_CLICKED, menuNavigateToPageHandler); 

     //Initialize SWFAddress 
      SWFAddress.addEventListener(SWFAddressEvent.INIT, initSWFAddress); 
     } 

    //Initialize SWFAddress and grab the page listed from the URL 
     private function initSWFAddress(e:SWFAddressEvent):void { 
     //Listen for change events 
      SWFAddress.addEventListener(SWFAddressEvent.CHANGE, URLNavigateToPageHandler); 

     //Fetch the page from the URL 
      var URL:String = SWFAddress.getValue(); 

      if (URL == "" || URL == "/") { 
       pagesResponder.token = page.getPagesByPosition(1); 
      } else { 
       pagesResponder.token = page.getPagesByURL(URL.substring(1)); 
      } 
     } 

    /** 
    * Navigation handlers 
    * ------------------------------- 
    */ 

    //Go to a specific page when a menu item has been clicked 
     private function menuNavigateToPageHandler(e:MenuEvent):void { 
      SWFAddress.setValue(e.pageURL); 
      pagesResponder.token = page.getPagesByID(e.pageID); 
     } 

    //Request a page when the URL has changed 
     private function URLNavigateToPageHandler(e:SWFAddressEvent):void { 
      var URL:String = SWFAddress.getValue(); 

      if (URL == "" || URL == "/") { 
       pagesResponder.token = page.getPagesByPosition(1); 
      } else { 
       pagesResponder.token = page.getPagesByURL(URL.substring(1)); 
      } 
     } 

    /** 
    * Loading content 
    * ------------------------------- 
    */ 

    //Show an error dialog in the case of an error when communicating with the server 
     private function requestErrorHandler(e:FaultEvent):void { 
      Alert.show("Fault string: " + e.fault.faultString + "\nFault detail: " + e.fault.faultDetail, e.fault.faultCode); 
     } 

    //Determine what kind of page will be constructed 
     private function determinePage(e:ResultEvent):void { 
      var pageType:String = pagesResponder.lastResult.type; 

     //We will need to know the type of page to build 
      switch(pageType) { 
       case "menu" : 
        menuResponder.token = menuFetch.getMenuByType(pagesResponder.lastResult.category); 
        break; 

       case "lunch" : 
        break; 

       case "reviews" : 
        reviewsResponder.token = reviews.getAllReviews(); 
        break; 

       case "home" : 
       default : 
        break; 
      } 
     } 

    //Using the data that was fetched from the "menuFetch" service, construct the page of type menu 
     private function constructMenu(e:ResultEvent):void { 
     //Transition the existing page out of view 
      if (this.pageObject != null) { 
       Alert.show("i"); 
       TweenMax.to(this.pageObject, 0.75, { 
        alpha : 0, 
        y : 20 
       }); 


       removeElement(this.pageObject); 
      } 

      setTimeout(function():void { 
       this.pageObject = new FoodMenu(); 
       this.pageObject.alpha = 0; 
       this.pageObject.y = 20; 
       this.pageObject.data = menuResponder.lastResult; 
       addElement(this.pageObject); 

      //Tween the new menu into place 
       TweenMax.to(this.pageObject, 0.75, { 
        alpha : 1, 
        y : 0 
       }); 
      }, 1000); 
     } 
    ]]> 
</fx:Script> 

<!-- Make a request to the server for the page data --> 
<fx:Declarations> 
    <pagesservice:PagesService id="page"/> 
    <s:CallResponder id="pagesResponder" fault="requestErrorHandler(event)" result="determinePage(event)"/> 

    <menuservice:MenuService id="menuFetch"/> 
    <s:CallResponder id="menuResponder" fault="requestErrorHandler(event)" result="constructMenu(event)"/> 

    <reviewsservice:ReviewsService id="reviews"/> 
    <s:CallResponder id="reviewsResponder" fault="requestErrorHandler(event)" result="constructMenu(event)"/> 
</fx:Declarations> 
</s:Application> 
+0

우리는 당신을 말할 수 없다. 또한 setTimeout이 호출됩니다. 페이지의 다른 요소와 어떻게 관련이 있습니까? 더 많은 정보를 얻으려면 완전한 구성 요소 (MXML 파일)를 제공해야합니다. – JeffryHouser

+0

@ www.Flextras.com 관련 응용 프로그램의 전체 MXML이 추가되었습니다. –

+0

나는 물건이 왜 발생 하는지를 알아 내기 위해 코드를 해독하는 데 어려움을 겪고있다. 문제를 나타내는 작고 완전한 코드 샘플을 제공해야 할 수도 있습니다. – JeffryHouser

답변

2

내가 보는 한 가지 문제는이 코드 조각입니다에게로 전체 MXML 객체)를 닫습니다.

은 자기를 참조하려면 다음을 시도해보십시오 pageObject이 항상 NOT NULL 우리가 constructMenu가 호출 될 때 어떻게/모르면 등록 이유

trace(this); 
setTimeout(function():void { 
    trace(this); 
},1000); 
+2

이것은 정확하게 문제가되는 것입니다 ... 놀랍지 만'setTimeout()'을 사용하지 않습니다. 익명 함수 ('setTimeout()'에 대한 호출에서 선언 된)'this'는 OP가 기대하는 클래스가 아닌 전역 객체입니다. ** if ** 익명/인라인 함수를'setTimeout()'과 함께 사용하지 않고 클래스에서 선언 한 함수에 대한 참조 만 전달하면됩니다. –

+0

@SunilD. 정말로 정말로 별 났지만 알고있는 것이 좋다! 나는 그것이 AS가 JS와 다르다는 것을 짐작한다! 저것을 시도하게하십시오. –

+0

@ Sun Di. 완전히 일했다! 둘 다 감사합니다! –