3

In one of my pages I have a link that is handled by pjax. Basically the user clicks an item, this item becomes "checked" (and saved in the DB).

I have disabled pushState for these requests, because it makes no sense, user effectively stays in the same page, so it's counter-intuitive to change the url.

However, there is a case when this pjax request results in redirect to login page (when the user is not logged in). And this is when I really need pushState to work, and it doesn't because I disabled it in the first place.

Would it be possible to configure pjax in such a way that normal responses work without pushState, but redirect responses (done with X-Pjax-Url header) do perform pushState?

4
  • 1
    You can easily handle 2 different pjax config in the same page. Please add some details about how you are using pjax.
    – soju
    Oct 15, 2015 at 22:23
  • Sure, I could make two different pjax blocks, one with redirection, one without, but I need one link to behave differently depending on server's reply code. Actually, I've looked at the pjax's source code and I now doubt it's possible, because it always does a replaceState if response code is 301, 302 or 303. Oct 16, 2015 at 7:46
  • You don't need another pjax "block"
    – soju
    Oct 16, 2015 at 8:05
  • I know this is an old post, but you can use the linkSelector to define a jQuery selector to use pjax only on specific button (I do that for state change toggles and delete buttons)
    – MoVod
    Jun 14, 2020 at 23:50

2 Answers 2

4

There's no way to do it using current functionality. I've added two more options to pjax and my PR has been accepted to yii2 branch of pjax. So, without further ado:

https://github.com/yiisoft/jquery-pjax

//pushRedirect - Whether to pushState the URL for redirects. Defaults to false.
//replaceRedirect - Whether to replaceState the URL for redirects. Defaults to true.

// ...

jQuery(document).pjax("#example_selector", {
  "push": false,
  "replace": false,
  "pushRedirect": true,
  "replaceRedirect": false
});
0

Use a linkSelector to specify which links trigger the pjax calls

<?php Pjax::begin([
    'enablePushState' => false, // don't change the Browser URL
    'linkSelector' => 'pjax-btn', // pjax links that 
]); ?>

<?= GridView::widget([
    //...
    [ // no pjax, normal linl
        'label' => 'link with state replace',
        'format' => 'raw',
        'value' => function ($model, $key, $index, $column) {
            return Html::a($model->title, Url::to(['/controllet/action', 'id' => $model->id]));
        }
    ],
    [
        'class' => 'yii\grid\ActionColumn',
        //'class' => 'common\widgets\ActionColumn',
        'template' => '{toggle} {view} {update} {delete}',
        'buttons' => [
            'toggle' => function ($url, $model, $key) {
                $options = [
                    'title' => 'Privacy',
                    'aria-label' => 'Privacy',
                    'class' => 'pjax-btn', // no state replacement, load with pjax
                ];
                $icon = Html::tag('span', '', ['class' => 'glyphicon glyphicon-check']);
                return Html::a($icon, $url, $options);
            },
        ]
    ]]) ?>
 <?php Pjax::end(); ?>   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.