Change JqueryAsset's jQuery version on certain

2019-08-08 16:02发布

I have one asset bundle (AppAsset) which registered in my main layout (main.php) for common 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 certain page (not on entire app!) i need to change my jquery to a lower version and hook up other assets. And i'm registeting another asset bundle which depends on main asset bundle AppAsset in the desired view file :

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's version in child asset bundle GalleryAsset or in one certain view file (page)?

Thank you and God bless helpers.

标签: php jquery yii2
1条回答
该账号已被封号
2楼-- · 2019-08-08 16:53

You can read how to customize asset bundles in offical documentation.

But customizing through application config is not suitable here, because at that moment requested route is unknown.

You can change the necessary asset bundle at runtime using 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'],
];

Make sure to place this code before registering asset bundle related with that page.

In that case all assets depending on yii\web\JqueryAsset will cause registering older version.

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

查看更多
登录 后发表回答