これは簡単なperiodic refresh JavaScriptメソッドを使用することによって行うことができます。メッセージインジケータを持つビューでこのよう
何か:
<?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の中にAJAXを知らない場合には、コントローラのアクションのために、以下の点を確認してください。
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'です。私はApache用にポート82を使用しているので、手動で新しいサーバー設定に適応するように変更するのは不愉快かもしれません。 – Silentbang
すごくいい、私も何かを学んだ。 –
ああ私は忘れてしまった。 「コントローラ/アクション」は、これをビューに実装したい場合にのみ適しています。 url:\ ""のように文字列を連結する必要があります。 CController :: createUrl( 'コントローラ/アクション')。 "\"、 – Silentbang