From 571b069953f559edd02f89476ebe628efa63d613 Mon Sep 17 00:00:00 2001 From: xue <> Date: Mon, 2 Apr 2007 21:46:04 +0000 Subject: Reduced ViewState size significantly by ignoring initial property values set in template --- framework/Web/UI/TControl.php | 55 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 10 deletions(-) (limited to 'framework/Web/UI/TControl.php') diff --git a/framework/Web/UI/TControl.php b/framework/Web/UI/TControl.php index 68e10c6e..5a8824d3 100644 --- a/framework/Web/UI/TControl.php +++ b/framework/Web/UI/TControl.php @@ -131,27 +131,35 @@ class TControl extends TApplicationComponent implements IRenderable, IBindable /** * @var string control unique ID */ - private $_uid=null; + private $_uid; /** * @var TControl parent of the control */ - private $_parent=null; + private $_parent; /** * @var TPage page that the control resides in */ - private $_page=null; + private $_page; /** * @var TControl naming container of the control */ - private $_namingContainer=null; + private $_namingContainer; /** * @var TTemplateControl control whose template contains the control */ - private $_tplControl=null; + private $_tplControl; /** - * @var TMap viewstate data + * @var array viewstate data */ private $_viewState=array(); + /** + * @var array temporary state (usually set in template) + */ + private $_tempState=array(); + /** + * @var boolean whether we should keep state in viewstate + */ + private $_trackViewState=true; /** * @var integer the current stage of the control lifecycles */ @@ -703,6 +711,17 @@ class TControl extends TApplicationComponent implements IRenderable, IBindable unset($this->_rf[self::RF_CONTROLSTATE][$key]); } + /** + * Sets a value indicating whether we should keep data in viewstate. + * When it is false, data saved via setViewState() will not be persisted. + * By default, it is true, meaning data will be persisted across postbacks. + * @param boolean whether data should be persisted + */ + public function trackViewState($enabled) + { + $this->_trackViewState=TPropertyValue::ensureBoolean($enabled); + } + /** * Returns a viewstate value. * @@ -714,7 +733,16 @@ class TControl extends TApplicationComponent implements IRenderable, IBindable */ public function getViewState($key,$defaultValue=null) { - return isset($this->_viewState[$key])?$this->_viewState[$key]:$defaultValue; + if(isset($this->_viewState[$key])) + return $this->_viewState[$key]!==null?$this->_viewState[$key]:$defaultValue; + else if(isset($this->_tempState[$key])) + { + if(is_object($this->_tempState[$key]) && $this->_trackViewState) + $this->_viewState[$key]=$this->_tempState[$key]; + return $this->_tempState[$key]; + } + else + return $defaultValue; } /** @@ -729,10 +757,16 @@ class TControl extends TApplicationComponent implements IRenderable, IBindable */ public function setViewState($key,$value,$defaultValue=null) { - if($value===$defaultValue) - unset($this->_viewState[$key]); - else + if($this->_trackViewState) + { $this->_viewState[$key]=$value; + unset($this->_tempState[$key]); + } + else + { + unset($this->_viewState[$key]); + $this->_tempState[$key]=$value; + } } /** @@ -742,6 +776,7 @@ class TControl extends TApplicationComponent implements IRenderable, IBindable public function clearViewState($key) { unset($this->_viewState[$key]); + unset($this->_tempState[$key]); } /** -- cgit v1.2.3