2010-06-16 76 views
2

讓我們假設我在頁面上呈現組件(比如Yahoo Finance等Graph)。組件視圖模板包含一堆我想要在圖形中切換週期的a_hrefs。我在Component中創建了事件和事件處理程序。我有兩個問題:如何提高Yii中的CComponent事件

  1. 如何通過那些a_hrefs引發圖組件事件(它們應該是Graph的一部分嗎?)?
  2. 如何重繪圖而不丟失頁面上下文(section,filter - 指定爲$ _GET值)?

我的圖形組件是這樣的:

Yii::import('zii.widgets.CPortlet'); 


class Graph extends CPortlet 
{ 
private $_period; 

/* **************************************** * 
*   COMPONENT PROPERTIES   * 
* **************************************** */ 

public function getPeriod() 
{ 
    return $this->_period; 
} 

public function setPeriod($period) 
{ 
    $this->_period = $period; 
} 

/* **************************************** * 
*     GENERIC     * 
* **************************************** */ 

public function init() 
{ 
    parent::init(); 

    // assign event handlers 
    $this->onPeriodChange = array($this, 'handlePeriodChange'); 
} 


protected function renderContent() 
{ 
    $this->render('graph'); 
} 

/* **************************************** * 
*     EVENTS     * 
* **************************************** */ 

public function onPeriodChange($event) 
{ 
    $this->raiseEvent('onPeriodChange', $event); 
} 

/* **************************************** * 
*    EVENT HANDLERS    * 
* **************************************** */ 

public function handlePeriodChange($event) 
{ 
    // CODE 
} 
} 

回答

0

可以提高這樣說:

$圖=新圖(); $ event = new CEvent($ graph); $ graph-> onPeriodChange($ event);

要重繪圖形,您應該收集通過$ _GET傳遞的參數,並在形成refresh()的url時再次使用它們。

1

我想你可以打電話,如果事件處理程序存在,因此調用事件

public function setPeriod($period) 
{ 
    if($this->hasEventHandler('onPeriodChange')) 
     $this->onPeriodChange($this); 
    $this->_period = $period; 
}