2

How to include my .js files after javascript code generated by Datepicker widget in view file.

            echo DatePicker::widget([
                'name'  => 'datepicker--2',
                'id' => 'datepicker--2',
                'clientOptions' => [
                    'showOtherMonths' => true,
                    'maxDate' => '+ 0d',
                    'showOtherMonths' => true,
                    'selectOtherMonths' => true,
                ]
            ]);

My asset bundle:

namespace app\assets;

use yii\web\AssetBundle;

class ChartsAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $js = [
        'js/charts.js',
        'js/charts-init.js',
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\jui\JuiAsset',
        'yii\web\JqueryAsset',
    ];
}

What I'm getting on my page source:

...
<script src="/new/js/charts.js"></script>
<script src="/new/js/charts-init.js"></script>
<script type="text/javascript">jQuery(document).ready(function () {
$('#datepicker--2').datepicker($.extend({}, {"showOtherMonths":true,"maxDate":"+ 0d","selectOtherMonths":true,"dateFormat":"M d, yy"}));
});</script></body>
</html>
Mihai P.
  • 9,307
  • 3
  • 38
  • 49
user1561346
  • 502
  • 3
  • 13
  • 28
  • 2
    Is there any reason that you want your scripts to be loaded after the `jQuery(document).ready()` code generated by the `DatePicker`? The code in there only gets executed after the DOM has loaded anyway, including the script tags that load your js files. – deacs Feb 18 '15 at 18:03
  • ^ what he asked, what problem are you trying to solve by doing this? – Mihai P. Feb 19 '15 at 00:49
  • @deacs charts-init.js contains jQuery(document).ready() too and need to access initiated datepicker. – user1561346 Feb 19 '15 at 12:06
  • When you register your js file. Define the depends $this->registerJsFile(Yii::$app->homeUrl .'test.js', [ 'depends' => [yii\web\JqueryAsset::className()], 'position' => \yii\web\View::POS_END ]); dependent on your datepicker assets class like i did for jqueryAssets – Kshitiz Feb 21 '15 at 08:32

2 Answers2

4

With yii2, you have options to define the position (HEAD, BEGIN OR END) of the client scripts with in the the document. This can be achieved by doing something like this

public $jsOptions = [
        'position' => \yii\web\View::POS_HEAD
 ];

Or Using This.

tnchalise
  • 1,573
  • 2
  • 18
  • 38
2

Create BaseAssets like this:

namespace app\assets;

class BaseAsset extends AssetBundle
{
    public $sourcePath = '@resources'; // @app/resources
    public $css = [];  
    public $js = [];    
    public $depends = [
        'yii\web\YiiAsset',
        'yii\jui\JuiAsset',
        'yii\web\JqueryAsset',
    ];         
    public $jsOptions = [ 'position' => \yii\web\View::POS_HEAD ];
}

And your Assets:

namespace app\assets;

use yii\web\AssetBundle;

class ChartsAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $js = [
        'js/charts.js',
        'js/charts-init.js',
    ];
    public $depends = [
        'app\assets\BaseAssets',        
    ];
    public $jsOptions = [ 'position' => \yii\web\View::POS_END ];
}
deacs
  • 4,259
  • 2
  • 27
  • 37
vitalik_74
  • 4,573
  • 2
  • 19
  • 27