Javascript 如何在Yii中重构AJAX进程

Javascript 如何在Yii中重构AJAX进程,javascript,php,ajax,yii,cgridview,Javascript,Php,Ajax,Yii,Cgridview,我需要重构或设计方面的帮助:)。我做了我的网络应用程序,它运行得很好。但是我仍然不能很好地理解Yii中的Ajax过程。例如,当我使用$.fn.yiiGridView.update()方法时,我看到了所有网页的返回,而不仅仅是CGridView的内容。这对我来说很有趣 但是现在:在索引视图中,我使用了CGridView,但是没有寻呼机!这是一个简单的赌博游戏webapp。在index.php页面上,我只查看了10次下注/结果,10秒后,我使用JavaScript查看下一次10次下注/结果,10秒后

我需要重构或设计方面的帮助:)。我做了我的网络应用程序,它运行得很好。但是我仍然不能很好地理解Yii中的Ajax过程。例如,当我使用
$.fn.yiiGridView.update()
方法时,我看到了所有网页的返回,而不仅仅是
CGridView
的内容。这对我来说很有趣

但是现在:在索引视图中,我使用了
CGridView
,但是没有寻呼机!这是一个简单的赌博游戏webapp。在index.php页面上,我只查看了10次下注/结果,10秒后,我使用JavaScript查看下一次10次下注/结果,10秒后再次查看下一次10次下注/结果:)

像这样的简单过程:

  • 调用actionIndex(),它呈现index.php(index.php 包含设计的JS代码,但不包含CGridView,其中 webapp(查看结果)
  • index.php rendert_ajaxIndex.php文件 内容
  • JS代码将计算接下来的10个结果,这些结果必须在网页上查看
  • 调用actionAjaxIndex()。这给了_ajaxIndex.php的新内容,重复3。再说一遍
  • JS代码再次计算接下来的10个Result
  • 注意:当管理员 在管理员网页中插入结果,webapp必须显示 临时结果。这就是为什么我需要刷新摘要和 在_ajaxIndex.php中舍入JS变量

    控制器

    /**
         * Lists all models.
         */
    public function actionIndex() {
        Yii::app()->language='hu';
    
        $model = new Result('search');
        $model->unsetAttributes();
    
        if (isset($_GET['Result']))
            $model->attributes = $_GET['Result'];
    
        if (isset($_POST['offset']) && $_POST['offset'] >= 0)
            $model->offset = $_POST['offset'];  
    
        $summary = Result::getCountSavedResults();
        $model->isLimited = true;
        $this->layout='projector'; 
    
        $this->render('index', array('model' => $model, 'summary'=>$summary));
        //$this->actionAjaxIndex();
    }
    
    /**
     * List all models by Ajax request.
     */    
    public function actionAjaxIndex() {
        Yii::app()->language='hu';
    
        $model = new Result('search');
        $model->unsetAttributes();  // clear any default values
    
        if (isset($_GET['Result']))
            $model->attributes = $_GET['Result'];
    
        if (isset($_POST['offset']) && $_POST['offset'] >= 0)
            $model->offset = $_POST['offset'];  
    
        $summary = Result::getCountSavedResults();
        $model->isLimited = true;
    
        $this->renderPartial('_ajaxIndex', array('model' => $model, 'summary'=>$summary));
    }
    
    我想在actionIndex()中终止此代码。但我不知道我该怎么做。。。我试图调用actionAjaxIndex等,但在调用actionAjaxIndex之前,我从Yii那里得到了PHP错误。(摘要变量不存在等)

    查看-Index.php

    <!--<h1><?php echo Yii::t('strings','Results'); ?></h1>-->
    <?php 
        echo CHtml::image(Yii::app()->request->baseUrl.'/images/toplista.jpg', "Fogadás");
    ?>
    <script type="text/javascript">
        // Initialize the variables for calculating
        var summary = <?php echo $summary ?>; // get all stored results
        var timeout = 10 * 1000; // in Milliseconds -> multiply with 1000 to use seconds
        var current = 0;
        var turn = 0;
        var rounds = Math.floor(summary / 10);
    </script>
    
    <?php $this->renderPartial('_ajaxIndex', array('model'=>$model, 'summary'=>$summary)); ?>
    
    <script type="text/javascript">
    
        // Refresh the CGridView's content in _ajaxIndex.php
        window.setInterval("refresh()", timeout);
        // Get the offset to the search() to set the criteria
        // Increase turn.
        function counter(){
            turn += 1;
    
            if(turn > rounds){
                turn = 0;
            }
            return turn *10;
        }
    
        function refresh() {       
            <?php
            echo CHtml::ajax(array(
                    'url'=> CController::createUrl("result/ajaxIndex"),
                    'type'=>'post',
                    'data'=> array('offset'=>'js: counter()'),
                    'replace'=> '#ajax-result-grid',
                    ))
            ?>
        }
    </script>
    
    <?php
    /* @var $model Result */
    ?>
    <?php $this->widget('zii.widgets.grid.CGridView', array(
            'id'=>'ajax-result-grid',
            'dataProvider'=>$model->search(),       
            'columns'=>array(
                    array(
                        'header'=>Yii::t('strings','No.'),
                        'value'=> $model->offset.' + $row+1',
                        'htmlOptions'=>array('style'=>'width:50px;'),
                     ),
                    array(
                        'header'=>Yii::t('strings','team_name'),
                        'name'=>'team_id',
                        'value'=>'$data->team->name'
                    ),
                array(
                        'header'=>Yii::t('strings','value'),
                        'name'=>'value',
                        'value'=>'$data->value'
                    ),
    
            ),
    )); ?>
    
    <script type="text/javascript">
        // This is need while the admins insert the results during this page is run.
        summary = <?php echo $summary ?>;
        rounds = Math.floor(summary / 10);
    </script>
    
    
    //初始化用于计算的变量
    变量摘要=;//获取所有存储的结果
    变量超时=10*1000;//以毫秒为单位->乘以1000以使用秒
    无功电流=0;
    var-turn=0;
    变量轮数=数学楼层(汇总/10);
    //在_ajaxIndex.php中刷新CGridView的内容
    setInterval(“刷新()”,超时);
    //获取搜索()的偏移量以设置条件
    //增加回合数。
    函数计数器(){
    圈数+=1;
    如果(旋转>旋转){
    匝数=0;
    }
    返回回合*10;
    }
    函数刷新(){
    }
    
    View-\u ajaxIndex.php

    <!--<h1><?php echo Yii::t('strings','Results'); ?></h1>-->
    <?php 
        echo CHtml::image(Yii::app()->request->baseUrl.'/images/toplista.jpg', "Fogadás");
    ?>
    <script type="text/javascript">
        // Initialize the variables for calculating
        var summary = <?php echo $summary ?>; // get all stored results
        var timeout = 10 * 1000; // in Milliseconds -> multiply with 1000 to use seconds
        var current = 0;
        var turn = 0;
        var rounds = Math.floor(summary / 10);
    </script>
    
    <?php $this->renderPartial('_ajaxIndex', array('model'=>$model, 'summary'=>$summary)); ?>
    
    <script type="text/javascript">
    
        // Refresh the CGridView's content in _ajaxIndex.php
        window.setInterval("refresh()", timeout);
        // Get the offset to the search() to set the criteria
        // Increase turn.
        function counter(){
            turn += 1;
    
            if(turn > rounds){
                turn = 0;
            }
            return turn *10;
        }
    
        function refresh() {       
            <?php
            echo CHtml::ajax(array(
                    'url'=> CController::createUrl("result/ajaxIndex"),
                    'type'=>'post',
                    'data'=> array('offset'=>'js: counter()'),
                    'replace'=> '#ajax-result-grid',
                    ))
            ?>
        }
    </script>
    
    <?php
    /* @var $model Result */
    ?>
    <?php $this->widget('zii.widgets.grid.CGridView', array(
            'id'=>'ajax-result-grid',
            'dataProvider'=>$model->search(),       
            'columns'=>array(
                    array(
                        'header'=>Yii::t('strings','No.'),
                        'value'=> $model->offset.' + $row+1',
                        'htmlOptions'=>array('style'=>'width:50px;'),
                     ),
                    array(
                        'header'=>Yii::t('strings','team_name'),
                        'name'=>'team_id',
                        'value'=>'$data->team->name'
                    ),
                array(
                        'header'=>Yii::t('strings','value'),
                        'name'=>'value',
                        'value'=>'$data->value'
                    ),
    
            ),
    )); ?>
    
    <script type="text/javascript">
        // This is need while the admins insert the results during this page is run.
        summary = <?php echo $summary ?>;
        rounds = Math.floor(summary / 10);
    </script>
    
    
    //这是管理员在运行此页面期间插入结果时需要的。
    总结=;
    轮数=数学楼层(汇总/10);
    

    是的,我想我不太清楚Yii:/。

    中的Ajax过程。事实上,您已经向前迈出了一大步,使用相同的模板--
    \u ajaxIndex.php
    ——用于Ajax调用和初始页面加载。但是,是的,你可以在这个方向上走得更远——也可以使用一个动作

    在这个操作中,应该检查是否通过AJAX调用了该方法。根据结果,您可以呈现整个页面,也可以仅呈现部分页面

    通常,这项检查只需简单到

    if (Yii::app()->request->isAjaxRequest) { ... }
    
    但是这里有一个问题:这个检查依赖于自定义HTTP头
    HTTP\u X\u REQUESTED\u WITH
    ,许多JS库使用它来实现自己的AJAX例程。遗憾的是,有些代理服务器会删除此标头,因此它可能不可靠。100%确定地说,只需在AJAX助手中提供另一个参数(最简单的是
    AJAX
    ),然后也检查这个参数。

    Ah感谢您的帮助:)。现在我将尝试isAjaxRequest():)。