Yii2 Pjax overwriting script files; filter not working

In using the Pjax widget baked into Yii2, I noticed my javascript files were being overwritten by the Pjax content’s script includes. For instance, the first page loads jQuery and then several jQuery plugins. When loading a new page via Pjax, the new content includes a new jQuery script tag, which load and overwrites the existing jQuery object, and all the plugins that were previously loaded no longer exist on the new jQuery object.

It appears there is some code that was written to try to only include javascript files if they haven’t already been loaded on the page using jQuery’s $.ajaxPrefilter():

(first post, so I wasn’t allowed to include a full URL/links; sorry)

URL to jQuery’s ajaxPrefilter: api.jquery.com/jquery.ajaxprefilter/

If I understand $.ajaxPrefilter() correctly, it only fires on AJAX calls. For Pjax requests (where HTML is returned), that would work. However, the script tags within the returned HTML does not appear to go through the same $.ajaxPrefilter() process since it’s not being pulled in over Ajax.

For reference, the original feature came from: github.com/yiisoft/yii2/pull/1149

I’m posting this here first because I feel like I’m missing something. Can someone confirm this?

Hi,

did you already find a solution for this problem? I have the same problem with loading a view with a gridview through pjax and filters aren’t working anymore.

Regards

Not a great one. I extend \yii\web\View:

config/main.php:


return [

    ...

    'components' => [

        'view' => ['class' => 'app\\components\\View'],

        ....

    ],

    ...

];

app/components/View.php:




namespace app\components;


class View extends \yii\web\View

{

    public function registerJsFile($url, $options = [], $key = null)

    {

        // ignore files when PJAX request; load them on the original page

        if (\Yii::$app->request->getIsPjax()) {

            return;

        }


        return parent::registerJsFile($url, $options, $key);

    }

}

Your namespace and directory structure choices may vary.

Thanks for the quick reply. But in my case this is also not working :(

In the div which get replaced by pjax i always have another instance of the javascript gridview code… (see pic)