Yii的:更新内容的小部件阿贾克斯 [英] Yii: Update content widget with ajax

查看:145
本文介绍了Yii的:更新内容的小部件阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有打造这就是所谓的每一页,给用户的通知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
   ));

这篇关于Yii的:更新内容的小部件阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆