Change order of registered script files in yii

别来无恙 提交于 2019-12-21 12:13:03

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!