Yii Framework Forum: Yii2 Pjax overwriting script files; filter not working - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Yii2 Pjax overwriting script files; filter not working Rate Topic: -----

#1 User is offline   spacker 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 04-September 13

Posted 15 May 2015 - 12:00 PM

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():
github.com/yiisoft/yii2/blob/2.0.4/framework/assets/yii.js#L311

(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?
0

#2 User is offline   pinoytagamunich 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 11-September 15

Posted 22 September 2015 - 03:47 PM

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
0

#3 User is offline   spacker 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 04-September 13

Posted 22 September 2015 - 04:32 PM

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

#4 User is offline   pinoytagamunich 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 11-September 15

Posted 22 September 2015 - 05:02 PM

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)

Attached File(s)


0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users