P
P
Pavel Gogolinsky2014-11-25 07:37:05
Yii
Pavel Gogolinsky, 2014-11-25 07:37:05

How to prevent scripts from reconnecting after ajax request in yii2?

On my page using yii2-editable ( demos.krajee.com/editable ) several records from the database are displayed. And there is a button for ajax adding a new entry. Using renderAjax, I update the list, but at the same time, repeated scripts are connected, and all yii2-editables stop working. How can you disable the addition of unnecessary scripts and how is yii generally thought out to refresh the page after an Ajax request?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Roman Frank, 2014-11-25
@Akellacom

I usually make an ajax request to some action in the controller, where I process the data and issue an updated page using renderPartial, it is returned without layout, which just does not generate additional scripts.

D
Dmitry Vapelnik, 2014-11-28
@dvapelnik

Akellacom says it all, but all this can be done in one controller - why do we need to produce another identical controller with the same logic, but with a partial page rendering. That is, you need to check whether Ajax is a request or not. If Ajax, then render only the view itself (renderPartial), if it is not Ajax, then render the entire page along with the headers.
In Yii1 it was done like this:

$viewFile = 'viewFile';
$viewData = array('model' => $model,);
if(Yii::$app->request->isAjaxRequest){
    $this->renderPartial($viewFile, $viewData);
}else{
    $this->render($viewFile, $viewData);
}

The fact is that all sorts of features like Ajax update page, if when they receive new data through an ajax request, they receive the entire page (header, sadbars, content, footers, CSS, JS files), then they cut out only the part that you need update and update it. In order for the user not to drive extra traffic on http, Ajax should check whether this is a request or not. Here is a link to the documentation: tyts , but there may be a problem due to the subtleties of the headers - tyts
Sorry, in Yii2 I have not had to do this yet, but I'm sure that the implementation is 90% similar to Yii1

V
Vadim Shurman, 2015-06-03
@VaShu

In Yii2 we checkif Yii::$app->request->isAjax

T
Timur Tuz, 2018-05-19
@TTA

for posterity)) just disable bundles that are not needed

if (Yii::$app->request->isAjax) {
            Yii::$app->assetManager->bundles = [
                'yii\bootstrap\BootstrapPluginAsset' => false,
                'yii\bootstrap\BootstrapAsset' => false,
                'yii\web\JqueryAsset' => false
            ];
            return $this->renderAjax('create', [
                'model' => $model,
            ]);
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question