在某些页面上更改JqueryAsset的jQuery版本 [英] Change JqueryAsset's jQuery version on certain page

查看:102
本文介绍了在某些页面上更改JqueryAsset的jQuery版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个资产捆绑包(AppAsset),已在我的主布局(main.php)中注册了整个应用程序的普通资产:

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',
    ];
}

但是在某个页面上(而不是整个应用程序上!),我需要将jquery更改为较低版本并连接其他资产.而且我在所需的视图文件中注册了另一个依赖于主要资产包AppAsset的资产包:

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',
    ];
}

问题是如何在子资产包GalleryAsset或某个特定视图文件(页面)中更改Jquery的版本?

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.

推荐答案

您可以在但是通过应用程序配置进行自定义不适用于此处,因为此时所请求的路由是未知的.

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

您可以在运行时使用 assetManager 组件:

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.

在这种情况下,取决于yii\web\JqueryAsset的所有资产都将导致注册较早的版本.

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).

这篇关于在某些页面上更改JqueryAsset的jQuery版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆