Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should Pjax be always registering javascript? #6917

Closed
nirvana-msu opened this issue Jan 18, 2015 · 8 comments
Closed

Should Pjax be always registering javascript? #6917

nirvana-msu opened this issue Jan 18, 2015 · 8 comments

Comments

@nirvana-msu
Copy link
Contributor

Spent quite a bit of time debugging an issue today. I was rendering a partial view in response to pjax request using renderPartial() and was surprised to see that some inline javascript was registered. Turned out this is because Pjax widget always executes $view->endBody(); and other similar View methods. Sometimes you may want to handle javascript on your own, especially for ajax updates of complex ListView widgets. I think it would be useful if it was possible to choose whether javascript should be registered during pjax request or not, just like you can choose so by using renderPartial() / renderAjax() during a normal ajax request

@alex-code
Copy link
Contributor

Are you registering the inline javascript?

You could move the registerJs() calls below the Pjax widget in the view as Pjax calls Yii::$app->end();

@nirvana-msu
Copy link
Contributor Author

The problem is in 3rd party code (widgets) that I do not have control of. They generate both inline javascript and register additional script files, which I ideally would like to avoid.

@nunomaduro
Copy link

I have the same problem. I use 3rd party code widgets and i would like to avoid register js for every case :s

@SilverFire
Copy link
Member

Fixed in yiisoft/jquery-pjax#30

@SilverFire SilverFire added this to the 2.0.7 milestone Nov 23, 2015
@SilverFire SilverFire self-assigned this Nov 23, 2015
@nirvana-msu
Copy link
Contributor Author

@SilverFire does it really fix this issue? I looked briefly through the commit / discussion, but it seems to be mostly related to the order of script execution. Whereas this issue is about having an option to avoiding loading scripts altogether. How does this patch help me achieve such a behaviour?

@SilverFire
Copy link
Member

@nirvana-msu Sorry, it seems I didn't get you idea correctly from the first read.

The patch does the following:

  • allows JS loading from <script src="..."> tags in PJAX response
  • cares about loading of the those scripts only one time
  • manages the order of load to prevent <script>...</script> execution before the loading of dependent JS sources

@SilverFire SilverFire reopened this Nov 24, 2015
@SilverFire SilverFire modified the milestones: 2.0.x, 2.0.7 Nov 24, 2015
@SilverFire
Copy link
Member

@nirvana-msu Following best practices, inline scripts should only initialize plugins, loaded in separated resource files and affect only the block, you load dynamically.
It's not OK to render in AJAX response something like $(document).on('click', 'a', function () { alert(1); });, you know.

Maybe I'll fix the problem with multiple loading of scripts, but I think we should do nothing special to inline JS - it should be just executed.

@SilverFire
Copy link
Member

Since now JavaScripts load through PJAX will be processed by jQuery.ajaxPrefiler yiisoft/jquery-pjax@7a47df7
Could you check, please?

@SilverFire SilverFire modified the milestones: 2.0.7, 2.0.x Nov 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants