其他分享
首页 > 其他分享> > 深夜 Yii2 排坑之pjax.

深夜 Yii2 排坑之pjax.

作者:互联网

最近在写一些基础demo ,偶尔用到了Yii:pjax . 结果用下来,一顿不爽。


依照网上度娘找了个例子  https://www.yiichina.com/tutorial/814 , 结果嫩是没有毛的效果。 再查看网页源代码 ,发现连 js 库都没加载。
这里就不讨论为啥前后端不分离了, 我就一小小的后端sde ,为自己的saas 写个demo 而以。
顺着源代码查了一下。

    /**
     * Registers the needed JavaScript.
     */
    public function registerClientScript()
    {
        $id = $this->options['id'];
        $this->clientOptions['push'] = $this->enablePushState;
        $this->clientOptions['replace'] = $this->enableReplaceState;
        $this->clientOptions['timeout'] = $this->timeout;
        $this->clientOptions['scrollTo'] = $this->scrollTo;
        if (!isset($this->clientOptions['container'])) {
            $this->clientOptions['container'] = "#$id";
        }
        $options = Json::htmlEncode($this->clientOptions);
        $js = '';
        if ($this->linkSelector !== false) {
            $linkSelector = Json::htmlEncode($this->linkSelector !== null ? $this->linkSelector : '#' . $id . ' a');
            $js .= "jQuery(document).pjax($linkSelector, $options);";
        }
        if ($this->formSelector !== false) {
            $formSelector = Json::htmlEncode($this->formSelector !== null ? $this->formSelector : '#' . $id . ' form[data-pjax]');
            $submitEvent = Json::htmlEncode($this->submitEvent);
            $js .= "\njQuery(document).off($submitEvent, $formSelector).on($submitEvent, $formSelector, function (event) {jQuery.pjax.submit(event, $options);});";
        }
        $view = $this->getView();
		
		
		/*应该是这砣代码出了问题,没注册成功**/
        PjaxAsset::register($view);

        if ($js !== '') {
            $view->registerJs($js);
        }
    }


    public $sourcePath = '@bower/yii2-pjax';
    public $js = [
        'jquery.pjax.js',
    ];
``

考虑到我是个懒人,算了一下,屁大点事,也就几十行代码,我犯不着去里三层,外三层的去折腾人家的代码。
万一改来改去,他其它依赖他这砣代码的东西一并干崩了,更是蛋疼。
直接自己写一个自己用就完事了。

namespace common\helpers;

class MyPjaxHelper
{
private static $pjax_id = null;

public static function begin($config = ['id' =>'pjax-content'])
{
    $pjax_id = $config['id'];
    self::$pjax_id = $pjax_id;
    echo "<div id='$pjax_id'>";
    $headers = \Yii::$app->getRequest()->getHeaders();
    if ($headers->get("X-PJAX") && $headers->get("X-PJAX-Container") == "#pjax-container") :
        ob_clean();
        ob_implicit_flush(false);
    endif;
}

public static function end()
{
    $pjax_id = self::$pjax_id;
    $headers = \Yii::$app->getRequest()->getHeaders();
    $conent =  ob_get_contents();
    ob_end_clean();
    echo $conent;
    if ($headers->get("X-PJAX") && $headers->get("X-PJAX-Container") == "#$pjax_id") {
          exit();
    }
    echo "<script>  $(document).pjax('a', '#$pjax_id')</script>";
}

}


使用方法:
```php
<?php
use \common\helpers\MyPjaxHelper;
ob_start();?>
    <script src="<?=obs_base()?>ace/js/jquery.pjax.min.js"></script>

 view 中局部刷新前面的内容 ..


<?php MyPjaxHelper::begin(['id' => 't1']);?>
    <a href="/edu/sign/demo?t=xuxing"><?php echo time();?> </a>
    <?php pr2($_REQUEST);
    echo microtime(true);
    ?>

<?php
 MyPjaxHelper::end();
?>

view code 局部刷新后面的内容 ..

写玩后,找了几个地方,自己套着用了下,哎亲生的就是好用。。。

标签:clientOptions,排坑,headers,formSelector,js,pjax,Yii2,id
来源: https://www.cnblogs.com/xuxing126/p/16217526.html