更改yii中已注册脚本文件的顺序 [英] Change order of registered script files in yii

查看:59
本文介绍了更改yii中已注册脚本文件的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Yii应用程序制作一个小部件.主布局视图注册所有通用脚本文件,例如jQuery和jQueryUI.在我的小部件中,我想使用依赖已加载的jQueryUI的jQueryUI插件.

I'm making a widget for a Yii application. The main layout view registers all the common script files such as jQuery and jQueryUI. In my widget I want to use a jQueryUI plugin that relies on jQueryUI already being loaded.

我知道我可以指定脚本在页面上的哪个位置,但是将其简单地包含在末尾"似乎有点不知所措-如果在该插件之后还需要加载其他脚本,该怎么办?如何确保以正确的顺序加载它们-任何人都有任何想法?

I know I can specify where on the page the script is included, but it seems a bit hit and miss to simply include it at the "end" - what if I have other script that I need to load after that plugin? How do I ensure they're loaded in the right order - anyone got any ideas?

推荐答案

您可以在Yii中使用依赖项功能脚本包.我之前有类似的问题.

You can use the dependency feature in Yii script packages. I was having similar problem before.

例如,您具有如下所示的脚本包配置,

For example you have script packages config like below,

'clientScript' => array(
  'packages' => array(
     'package1' => array(
          'basePath' => 'path.to.package1',
          'js' => array(
              'package1.js',
          ),
          'css' => array(
              'package1.css'
          ),
      ),
     'package2' => array(
          'basePath' => 'path.to.package2',
          'js' => array(
              'package2.js',
          ),
          'css' => array(
              'package2.css'
          ),
          'depends' => array(
                'package1',
          )
      ),
     'package3' => array(
          'basePath' => 'path.to.package3',
          'js' => array(
              'package3.js',
          ),
          'css' => array(
              'package3.css'
          ),
          'depends' => array(
                'package2',
          )
      ),
   )
)

在上面的示例中,package2要求(取决于)package1package3要求package2.假设您的情况是,小部件使用package2,其他脚本使用package3.即使不渲染窗口小部件,如果使用Yii::app()->clientScript->registerPackage('package3');,它也会自动安装package2,然后在package2之前安装package1(如果已经需要package1,则不会安装)之前有一些脚本.)

In sample above, package2 requires (depends) package1 and package3 requires package2. Let's say in your case, a widget uses package2, and the other script uses package3. Even if you don't render the widget, if you use Yii::app()->clientScript->registerPackage('package3');, it will automatically install the package2 which then install the package1 before package2 (or won't install if the package1 is already required by some scripts before.).

这篇关于更改yii中已注册脚本文件的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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