간단한 periodic refresh 자바 스크립트 메서드를 사용하여이 작업을 수행 할 수 있습니다. 메시지 표시등이 뷰에서이 같은
뭔가 :
<?php Yii::app()->clientScript->registerScript('autoupdate-div-inbox-update',
"setInterval(function(){
// do an ajax call to server to check for new messages
// using jquery's ajax method
$.ajax({
url: 'http://example.com/index.php?r=controller/action',// this is the url that will check for new messages for the current user
success: function(data) {// data is the data returned from the server
if(data){
// update your new message div
// you can show your red icon here
}
}
});
return false;
},1000);"
);
?>
그래서 일어나고있는 것은 setInterval을 방법은 기능마다 1000 밀리 초와 Ajax를 사용하여 새 메시지 기능 검사를 실행하는 것입니다.
당신이 YII에서 아약스를 모르는 경우
는 다음 컨트롤러 액션에 대해 다음 사항을 확인하십시오
public function actionMessages(){
// check for new messages in the db
$xyz = checkMessage();
// assuming checkMessage returns the number of new messages if any or false if none
// whatever we echo will be available to the javascript we wrote in the data variable
echo $xyz;
}
가
timing methods in javascript에 대해 자세히 알아보십시오.
이 패턴은 또한 폴링라고
, 내가 잘 알고 아니에요 롱 폴링 및 서버 푸시 같은 다른 인기있는 방법도있다,하지만 당신은 너무 패턴을 결정하기 전에 그들을 밖으로 검사한다 .
호프가 도움이 되었으면 설명이 있는지 물어보십시오.
감사합니다. url은 간단히 : url : 'controller/action'일 수 있습니다. 포트 82를 아파치에 사용하기 때문에 새로운 서버 설정에 맞게 수동으로 수정하는 것은 불편할 수 있습니다. – Silentbang
끝내주는, 나도 무언가를 배웠다. –
오, 잊어 버렸습니다. "컨트롤러/액션"은 뷰에서이를 구현하려는 경우에만 적합합니다. 우리는 url처럼 문자열을 concanate해야합니다 : \ "". CController :: createUrl ('컨트롤러/액션'). "\", – Silentbang