Yii2. How to register css when using pjax?

The question relates only to when working on yii2 framework.
To briefly explain the code. This is the code view.
\yii\widgets\Pjax::begin(['id' => 'pjax-container']);
if(Yii::$app->request->isPjax) {
 echo \yii\jui\DatePicker::widget([
 'name' => 'date',
 'value' => ",
} else {
 echo \yii\helpers\Html::button('Load Pjax', ['onclick' => "$.pjax.reload({container:'#pjax-container'});"]);

The bottom line is that when the page loads css jquery-ui will not load because it does not pass the condition Yii::$app->request->isPjax.
Because in the header of the page will not be referenced styles. Then when I do a reboot via pjax condition passes. And styles have loaded. But are only js files and js code.
And if I do not use pjax, and in the controller call renderAjax. In this case, everything works fine.
Even more simple example
\yii\widgets\Pjax::begin(['id' => 'pjax-container']);
if(Yii::$app->request->isPjax) {
 $this->registerCssFile('http://test.css'); // not logged...
 $this->registerJsFile('http://test.js'); // registers
echo \yii\helpers\Html::button('Load Pjax', ['onclick' => "$.pjax.reload({container:'#pjax-container'});"]);

Here is an example of work clip2net.com/s/3y8hHIO and the result of the query clip2net.com/s/3y8hMoH where we see that js checked, and CSS is not.
July 9th 19 at 13:52
1 answer
July 9th 19 at 13:54
1. Yii::$app->request->isPjax - this should be in the controller, not in view.
2. PJAX didn't come up with that would hide the blocks. Why then pjax. Do a normal render and use js to display the block with the datepiker-om or bootstrap collapse getbootstrap.com/javascript/#collapse
And if I do not use pjax, and in the controller call renderAjax. In this case, everything works fine.
- because such is done without the use of PJAX, You do understand why is it different from AJAX?
4. You have everything working exactly as it should. Just You want through the ass to scratch his nose, and the framework authors did not think that someone would think of such an idea.
1. No, not necessarily. Moreover, this example is specifically simplified. You criticize for the hello world text output directly from the controller =)
2. You are again attached for example, and why I assumed what I'm trying to hide the blocks. And I'm just as simply showed the problem of check styles when outputting content via pjax.
3. Unlike pjax from ajax about the difference php from yii2 =) working Principle of pjax is very simple. The addition of a dynamic element change pages, change the url using pushState. Additionally yii2 on request understands pjax requests and cuts of the content with pjax widget. Which by the way is not necessary.
4. I disagree with you. What content will return yii requests pjax script is a problem especially yii. And the fact that yii is based on the query cuts out a necessary part of it's only feature of yii. For which they should be commended. Consider 2 scenarios. 1. pjax should ONLY return the content. In this case, we do not need to register js files when prompted. And we get.
2. Yii needs to register all necessary scripts when prompted. And in this case we get a response to the registration js. But then why wasn't logged css.
In the example I pointed out that if some of the content we display via View::renderAjax then register js and css. What is logical and correct.
Now more real-world example of using pjax which will cause the specified me a problem. Again my example you do not like.
The object catalog. Using pjax, we display more of the object. And on the details page, we register the additional css needed to display this page. And we end up with the same problem. Css does not load. - gonzalo_Hay commented on July 9th 19 at 13:57
You gave an example detached from Your application and believe that you will get the answer for the problem, not by example? Yes, I criticized an example. Because You just brought him in and he breaks the principles of MVC and uses PJAX for other purposes. I have no idea what You have there in reality. - Henderson_Beat commented on July 9th 19 at 14:00
: title theme I wrote that I want to find out how to register css files when using pjax. Because yii is behaving very oddly. js files are registered, and no css. Although it is logical if nothing was recorded, or Vice versa, all as it makes renderAjax.
The example shows the check styles when rendering via pjax. I made even more simple example - gonzalo_Hay commented on July 9th 19 at 14:03
: Everything is logical. js connecting in body and css in the head. Pjax replaces part of the content, sometimes it is the only block, then js is not connected, in Your case, meshing js in the body, apparently. In General, when used properly, he shouldn't not cling to the wrong thing. - Henderson_Beat commented on July 9th 19 at 14:06
: Don't have to, but catchy. An example of the basic version of yii. Because nothing broken can not be. Because the use of a priori correct =)
Now, regarding registration in the head. Nice try, but no.
\yii\widgets\Pjax::begin(['id' => 'pjax-container']);
if(Yii::$app->request->isPjax) {
$this->registerJsFile('test.js', ['position' => \yii\web\View::POS_HEAD]);
} else {
echo \yii\helpers\Html::button('Load Pjax', ['onclick' => "$.pjax.reload({container:'#pjax-container'});"]);
registers js, css, no. - gonzalo_Hay commented on July 9th 19 at 14:09
In General, all became on the places. This is a bug in yii2 which is something like decided cleansing styles.
yii\widgets\Pjax - 144 row
// Do not re-send css files as it may override the css files that were loaded after them.
// This is a temporary fix for https://github.com/yiisoft/yii2/issues/2310
// It should be removed once pjax supports loading only missing css files
$view->cssFiles = null;
And if we will render using renderAjax styles are duplicated, and if they are using pjax them clean. Generally, in one case scored. And the other shut down everything. - gonzalo_Hay commented on July 9th 19 at 14:12

Find more questions by tags YiiPHP