nguyendh
(Duynguyen0511)
May 14, 2015, 10:01pm
1
I put Select2 lib into Select2Asset
namespace frontend\assets;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Select2Asset extends AssetBundle
{
public $sourcePath = '@bower/select2';
public $baseUrl = '@web';
public $css = [
'dist/css/select2.min.css',
];
public $js = [
'dist/js/select2.min.js',
];
}
and in my view, I call it
use frontend\assets\Select2Asset;
Select2Asset::register($this);
the problem is select2.js is inserted before jquery.js (which handled by AppAsset.php)
<script src="/assets/24512da9/dist/js/select2.min.js"></script>
<script src="/assets/717f72d7/jquery.js"></script>
<script src="/assets/b02571ea/yii.js"></script>
<script src="/assets/b02571ea/yii.validation.js"></script>
<script src="/assets/b02571ea/yii.activeForm.js"></script>
<script src="/assets/db3e6b0f/js/bootstrap.js"></script>
How do I make select2.min.js appear after jquery.js ? I dont want to add Select2 to AppAsset as not all pages required Select2 lib and I am not looking for 3rd party like yii2-widget-select2.
Thanks
You need to add a dependency to yii’s assets you can read about it here.
{
public $sourcePath = '@bower/select2';
public $baseUrl = '@web';
public $css = [
'dist/css/select2.min.css',
];
public $js = [
'dist/js/select2.min.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset'
];
}
Your scripts will load after the $depends=>[] assets.
nguyendh
(Duynguyen0511)
May 15, 2015, 4:30pm
3
skworden:
You need to add a dependency to yii’s assets you can read about it here.
{
public $sourcePath = '@bower/select2';
public $baseUrl = '@web';
public $css = [
'dist/css/select2.min.css',
];
public $js = [
'dist/js/select2.min.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset'
];
}
Your scripts will load after the $depends=>[] assets.
Thanks, it solves my problem.
I have another question
<script src="/assets/717f72d7/jquery.js"></script>
<script src="/assets/b02571ea/yii.js"></script>
<script src="/assets/24512da9/dist/js/select2.min.js"></script>
<script src="/assets/b02571ea/yii.validation.js"></script>
<script src="/assets/b02571ea/yii.activeForm.js"></script>
<script src="/assets/db3e6b0f/js/bootstrap.js"></script>
I dig through the documentation and I dont understand where the yii.validation.js , yii.activeForm.js and bootstrap.js coming from ?
They are not coming from ‘yii\web\YiiAsset’ and ‘yii\bootstrap\BootstrapAsset’ for sure. Who call them ?
yii.activefrom.js would be cause you are using a activeFrom / activeField widget and the widget registers the js.
yii.validation.js is because of the above as well, i believe you can turn this off by setting clientValidation to false.
I’d read the docs again if i were you.
yii\bootstrap\BootstrapAsset’ is the boostrap css
yii\boostrap\BoostrapPluginAsset is the boostrap js