Reorder registered script files in yii

I am making a widget for a Yii application. The main layout view registers all regular script files such as jQuery and jQueryUI. In my widget, I want to use the jQueryUI plugin, which relies on the already loaded jQueryUI.

I know that I can indicate where the script is included on the page, but it seems that it hit and missed a bit by simply including it at the end - what if I have another script that I need to load after this plugin? How can I make sure they are loaded in the correct order - does anyone have any ideas?

+6
source share
1 answer

You can use the dependency function in Yii script packages . I had a similar problem before .

For example, you have script packages, as shown 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 the above example, package2 requires (depends) package1 and package3 requires package2 . Say, in your case, the widget uses package2 , and another script uses package3 . Even if you do not render the widget, if you use Yii::app()->clientScript->registerPackage('package3'); , it will automatically install package2 , which will then install package1 to package2 (or will not install if package1 is already required in some scenarios before.).

+11
source

Source: https://habr.com/ru/post/918542/


All Articles