2014-07-22 8 views
1

모든 페이지라고하는 Yii 프레임 워크에 알림 위젯을 빌드하고 사용자에게 알림을 제공합니다. 이제 Ajax를 사용하여 10 초마다 알림을 자동으로 업데이트하고 싶습니다. 구조는 다음과 같습니다.Yii : Ajax로 컨텐츠 위젯 업데이트

<?php 

class NotificationsWidget extends CWidget { 

public function init(){ 

} 
public function run() { 

} 

가장 적합한 방법은 무엇입니까? 나는 도처에서 수색했지만 대답을 찾을 수없는 것 같습니다. 어쩌면 나는 틀린 키워드로 다만보고있다. 누군가에는 이것을하는 다른 (더 나은) 방법이있는 경우에, 제발! 인터페이스 레이아웃에로드되어 적어도 10 초마다 업데이트되어야한다는 제한이 있습니다.

고마워요 :)

답변

2

당신 설치 컨트롤러의 행동과 반환 더 업데이트 아무것도 존재하지 않는 경우는, 부분보기에서 알림을 반환 업데이트가있는 경우, 그것을 10 초마다 폴링, 이것은 여러분에게 아이디어를주기위한 뼈대 구현입니다. 당신의 알림 컨트롤러에서 레이아웃 파일

... 
// Your normal layout content 

<?php Yii::app()->clientScript->registerScript("poll_ajax_notifications", 
'function getNotification(){'. 
    CHtml::ajax(array(
     'url'=>array("//notifications/update"), 
     'dataType'=>'html', 
     'type'=>'GET', 
     'update'=>'#divcontainingNotificationWidget', 
     ) 
    ) . '. } 
    timer = setTimeout("getNotification()", 10000); 
    ', CClientScript::POS_END); 

에서

class NotificationsController extends CController { 
.... 
public function actionUpdate(){ 
    $user = Yii::app()->user->id; 
    // Your logic logic for finding notifications 
    if($notificationPresent){ // or any validation to check whether to push data or not 
     $this->renderPartial('_notificationWidget',array('widgetData'=>$widgetData)); // pass data required by widget here 
    } 
    Yii::app()->end(); 
    } 
... 
} 

마지막으로/알림보기에 _notificationsWidget.php 이 위젯 전화를 걸라는 폴더 뷰에서 부분 뷰를 생성

<?php 
    $this->widget('path.to.my.widget',array(
    //widget parameters 
    )); 
+1

고맙습니다. 나는 이것으로 확실히 일할 수있다. – Shark