更改 yii 中已注册脚本文件的顺序 [英] Change order of registered script files in 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
需要(取决于)package1
和 package3
需要 package2
.假设在您的情况下,一个小部件使用 package2
,另一个脚本使用 package3
.即使你不渲染widget,如果你使用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屋!