Yii Framework Forum: duplication of CSS and JS files when using renderAjax() - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

duplication of CSS and JS files when using renderAjax()

#1 User is offline   swprqa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 17-January 15

Posted 07 February 2015 - 06:20 PM

Hi!
When i'm rendering view file by renderAjax() i have duplicated JS and CSS files, such as jQuery.js, bootstrap.css etc. How can i remove them?
0

#2 User is offline   flarpy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 158
  • Joined: 25-May 10

Posted 08 February 2015 - 05:23 AM

If you are rendering a partial from a controller method, just use return rather than render, eg

public function actionTest
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

other code here

return $yourResults;// automatically json_encoded
}

0

#3 User is offline   swprqa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 17-January 15

Posted 08 February 2015 - 07:48 AM

I'm rendering widgets like depdrop and other, and i need their JS and CSS assets. I just want to know how i can remove manualy some of assets from controller or view.
Well, i went to dependencies of thoose widgets and commented them (there was bootstrap and like that which are already included), so now i'm not having duplicated assets but i don't think it is the right way.
0

#4 User is offline   Roberto Braga 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 216
  • Joined: 18-February 13
  • Location:Roma, Italy

Posted 08 February 2015 - 10:33 AM

This is something you can't really avoid.
renderAjax does not know (and can't know) which js and css have already been included in the calling page.

You can try to do some script unregister before the output is sent to the client but I do no t think is worth the effort.
The browser already loaded and cached these script and it won't load them again.
1

#5 User is offline   swprqa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 17-January 15

Posted 08 February 2015 - 12:02 PM

Well, thanks for advice.
0

#6 User is offline   flarpy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 158
  • Joined: 25-May 10

Posted 08 February 2015 - 05:29 PM

You can create a hidden depdrop and add "use kartik\widgets\DepDrop;" to your controller, this will include all the files it needs, not pretty but it works!
0

#7 User is offline   Toms K 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 02-April 14

Posted 23 December 2017 - 02:25 PM

If you know you do not need to load jQuery and/or JUI within the view file, just put at the end:

unset($this->assetBundles['yii\web\YiiAsset']);
unset($this->assetBundles['yii\web\JqueryAsset']);

so the renderAjax method loads just the assets that are left..
0

#8 User is offline   skworden 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 525
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 01 January 2018 - 06:20 PM

I personally handle this in a common controller (I also use this controller to manage RBAC so you don't have to do it in a lot of places) i made that overrides the default render function. It works really well for my use and it sounds like it will for you as well. You would make your controllers extend your custom controller like the example below.


namespace common\controllers;

use Yii;

class Controller extends \yii\web\Controller {

    /**
 	*
 	* @inherdocs
 	*/
    public function render($view, $params = []) {
        if (Yii::$app->request->isAjax) {
            Yii::$app->assetManager->bundles = [
                'yii\bootstrap\BootstrapPluginAsset' => false,
                'yii\bootstrap\BootstrapAsset' => false,
                'yii\web\JqueryAsset' => false
            ];
            return $this->renderAjax($view, $params);
        }
        $content = $this->view->render($view, $params, $this);
        return $this->renderContent($content);
    }

}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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