问题
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?
回答1:
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',
)
),
)
)
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.).
来源:https://stackoverflow.com/questions/11116312/change-order-of-registered-script-files-in-yii