我有打造这就是所谓的每一页,给用户的通知Yii框架的通知部件。现在,我想自动更新的通知,每10秒使用Ajax。的结构如下:
I have build a notification widget in Yii framework which is called every page and gives the user notifications. Now I would like to update those notifications automatically every 10 seconds with Ajax. The structure is as follows:
<?php
class NotificationsWidget extends CWidget {
public function init(){
}
public function run() {
}
什么是做到这一点的最好方法是什么?我已经到处找,但我似乎无法找到答案。也许我只是在寻找与错误的关键词,如果还有人有另一种(更好的)办法做到这一点,请!唯一的限制是它必须装入界面布局和更新至少每10秒。
What is the best way to do this? I have searched everywhere, but I cannot seem to find the answer. Maybe I'm just looking with the wrong keywords.. If anyone has another (better) way to do this, please! Only restriction is that it has to be loaded into the interface layout and update at least every 10 seconds.
多谢:)
您安装在你的控制器动作和轮询它每10秒,如果有更新它会返回通知从局部来看,如果没有更新没有返回,这是一个框架实现,给你一个想法,注意它不会工作不变。
You setup an action in your controller and poll it every 10 seconds, if there is update it will return the notification from a partial view, if there is no update nothing is returned, this is a skeleton implementation to give you an idea, note it will not work as is.
在您的布局文件
...
// 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
您认为把你的插件调用
Finally create a partial view in views/notifications folder called _notificationsWidget.php
In your view place your widget call
<?php
$this->widget('path.to.my.widget',array(
//widget parameters
));
上一篇:如何使用Ajax使用的第一个选择框的值更新jQuery的第二个选择框?第一个、第二个、如何使用、Ajax
下一篇:将URL添加到一个iFrame基于IFrame的ID被称为Fancybox2阿贾克斯被称为、IFrame、URL、iFrame