flex 驗證-提交模式 源碼解析

來源:互聯網
上載者:User

一、設定組件的屬性

在組件裡會有這樣的代碼:

public function set XXX(value):void{

      if(this._XXX != value){

          this._XXX=value;

         invalidateProerties();

      }

}

1.調用UIComponent類的invalidateProperties方法:

public function invalidateProperties():void    {        if (!invalidatePropertiesFlag)        {            invalidatePropertiesFlag = true;            if (nestLevel && UIComponentGlobals.layoutManager)                UIComponentGlobals.layoutManager.invalidateProperties(this);        }    }

2.如上述代碼所示,高用LayoutManager的invalidateProperties方法

 public function invalidateProperties(obj:ILayoutManagerClient ):void    {        if (!invalidatePropertiesFlag && systemManager)        {            invalidatePropertiesFlag = true;            if (!listenersAttached)                attachListeners(systemManager);        }        // trace("LayoutManager adding " + Object(obj) + " to invalidatePropertiesQueue");        if (targetLevel <= obj.nestLevel)            invalidateClientPropertiesFlag = true;        invalidatePropertiesQueue.addObject(obj, obj.nestLevel);        // trace("LayoutManager added " + Object(obj) + " to invalidatePropertiesQueue");    }

3.attachlisteners方法給systemManager添加監聽

public function attachListeners(systemManager:ISystemManager):void    {        if (!waitedAFrame)        {            systemManager.addEventListener(Event.ENTER_FRAME, waitAFrame);        }        else        {            systemManager.addEventListener(Event.ENTER_FRAME, doPhasedInstantiationCallback);            if (!usePhasedInstantiation)            {                if (systemManager && (systemManager.stage || usingBridge(systemManager)))                {                    systemManager.addEventListener(Event.RENDER, doPhasedInstantiationCallback);                    if (systemManager.stage)                        systemManager.stage.invalidate();                }            }        }        listenersAttached = true;    }

4.在觸發render事件的時候,回調

 private function doPhasedInstantiationCallback(event:Event):void    {        // if our background processing is suspended, then we shouldn't do any         // validation        if (UIComponentGlobals.callLaterSuspendCount > 0)            return;                systemManager.removeEventListener(Event.ENTER_FRAME, doPhasedInstantiationCallback);        systemManager.removeEventListener(Event.RENDER, doPhasedInstantiationCallback);        if (!UIComponentGlobals.catchCallLaterExceptions)        {            doPhasedInstantiation();        }        else        {            try            {                doPhasedInstantiation();            }            catch(e:Error)            {                // Dispatch a callLaterError dynamic event for Design View.                 var callLaterErrorEvent:DynamicEvent = new DynamicEvent("callLaterError");                callLaterErrorEvent.error = e;                callLaterErrorEvent.source = this; callLaterErrorEvent.object = currentObject;                systemManager.dispatchEvent(callLaterErrorEvent);            }        }currentObject = null;    }

 

private function doPhasedInstantiation():void    {        // trace(">>DoPhasedInstantation");        // If phasing, do only one phase: validateProperties(),        // validateSize(), or validateDisplayList().        if (usePhasedInstantiation)        {            if (invalidatePropertiesFlag)            {                validateProperties();                // The Preloader listens for this event.                systemManager.document.dispatchEvent(                    new Event("validatePropertiesComplete"));            }            else if (invalidateSizeFlag)            {                validateSize();                // The Preloader listens for this event.                systemManager.document.dispatchEvent(                    new Event("validateSizeComplete"));            }            else if (invalidateDisplayListFlag)            {                validateDisplayList();                // The Preloader listens for this event.                systemManager.document.dispatchEvent(                    new Event("validateDisplayListComplete"));            }        }        // Otherwise, do one pass of all three phases.        else        {            if (invalidatePropertiesFlag)                validateProperties();            if (invalidateSizeFlag)                validateSize();            if (invalidateDisplayListFlag)                validateDisplayList();        }        // trace("invalidatePropertiesFlag " + invalidatePropertiesFlag);        // trace("invalidateSizeFlag " + invalidateSizeFlag);        // trace("invalidateDisplayListFlag " + invalidateDisplayListFlag);        if (invalidatePropertiesFlag ||            invalidateSizeFlag ||            invalidateDisplayListFlag)        {            attachListeners(systemManager);        }        else        {            usePhasedInstantiation = false;listenersAttached = false;var obj:ILayoutManagerClient = ILayoutManagerClient(updateCompleteQueue.removeLargest());            while (obj)            {                if (!obj.initialized && obj.processedDescriptors)                    obj.initialized = true;                if (obj.hasEventListener(FlexEvent.UPDATE_COMPLETE))                    obj.dispatchEvent(new FlexEvent(FlexEvent.UPDATE_COMPLETE));                obj.updateCompletePendingFlag = false;                obj = ILayoutManagerClient(updateCompleteQueue.removeLargest());            }            // trace("updateComplete");            dispatchEvent(new FlexEvent(FlexEvent.UPDATE_COMPLETE));        }        // trace("<<DoPhasedInstantation");    }

注釋:

1,在組件調用invalidateProperties()方法後,調用UIComponent的invalideProperties()。

2,再調用LayoutManager的invalidateProperties()方法。

3,給systemManager添加Render事件(必需要stage.invalidate(),此事件才可以觸發),在將來要更新和呈現顯示列表的時候調用。

4,REnder事件被觸發後,調用UIcomponent的validateProperties()方法。

5,再調用commitProperties()方法。通常在自訂群組件的時候會複寫該方法,將驗證-提交延遲到此方法中。改寫一些屬性,達到局部重新整理的效果。如:

override      protected      function commitProperties(): void      {      super.commitProperties();      if (sourceChanged){      if (! this.image){     image =  new Image();        this.addChild(image);     image.source =  this._source;        } else {     image.source =  this._source;      }     sourceChanged =  false ;      }      if (scaleChanged){      this.imageWidth =  this.imageWidth *   this.scale;      this.imageHeight =  this.imageHeight *+  this.scale;     scaleChanged =  false ;      }         }  

 

 

invalidateSize ----> measure和invalidateDisplayList   --------> updateDisplayList和上述的過程是一樣的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.