Changing jquery version of jqueryasset on a specific page

I have one asset package (AppAsset) that is registered in my main layout (main.php) for shared assets for the entire application:

class AppAsset extends AssetBundle { public $basePath = '@webroot'; public $baseUrl = '@web'; public $css = [ 'css/style.css', ]; public $depends = [ 'yii\web\JqueryAsset', 'yii\bootstrap\BootstrapAsset', ]; } 

But on one specific page (and not on the whole application!) I need to change my jquery to a lower version and connect other assets. And I register another asset package, which depends on the main AppAsset asset package in the desired file of the form:

 class GalleryAsset extends AssetBundle { public $basePath = '@webroot'; public $baseUrl = '@web'; public $css = [ 'css/gamma/style.css', 'css/gamma/noJS.css', ]; public $js = [ 'js/gamma/modernizr.custom.70736.js', 'js/gamma/jquery.masonry.min.js', 'js/gamma/jquery.history.js', 'js/gamma/js-url.min.js', 'js/gamma/jquerypp.custom.js', 'js/gamma/gamma.js', ]; public $depends = [ 'app\assets\AppAsset', ]; } 

The question is, how can I change the jQuery version in the GalleryAsset set of child assets or in one specific file (page)?

Thank you and God bless the helpers.

+1
source share
1 answer

You can read how to set up asset packages in the official documentation .

But the setting using the application configuration is not suitable here, because at this moment the requested route is unknown.

You can change the required set of assets at runtime using the assetManager component:

 use Yii; ... Yii::$app->assetManager->bundles['yii\web\JqueryAsset'] = [ 'sourcePath' => null, 'js' => ['jquery.js' => 'https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.js'], ]; 

Be sure to put this code before registering the asset package associated with this page.

In this case, all assets depending on yii\web\JqueryAsset will result in the registration of an older version.

I also recommend that you additionally check the compatibility of the plugins used with this version (it's not that old, though).

+1
source

Source: https://habr.com/ru/post/983149/


All Articles