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)
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.
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.
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.