How to make the registerJS method code to depend on AppAsset

I have the AppAsset connected to the main js file of the website;It contains some global functions which I use in view registerJS (being executed at the end: POS_END) javascript code.

What I have noticed is, the code in file(AppAsset bundle) is being execute after registerJS’ code. I would like the code from the file to be first executed then registerJS.

How can I make that happen?

My AppAsset.php file in basic app


<?php


namespace app\assets;


use yii\web\AssetBundle;


class AppAsset extends AssetBundle

{

    public $basePath = '@webroot';

    public $baseUrl = '@web';

    public $css = [

        'css/site.css',

    ];

    public $js = [

        'js/ajax-modal-popup.js',

    ];

    public $depends = [

        'yii\web\YiiAsset',

        // ...

    ];

}

And in view


<?php

$js = <<<JS

    function cb(start, end) {

        // Code

    }

JS;

$this->registerJs($js);

?> // OR

<?php

// Include JS or Css Files in view page

$this->registerJsFile('//path-to-js-file.js', ['depends' => [yii\web\JqueryAsset::className()]]);

$this->registerCssFile('//path-to-css-file.css', ['depends' => [yii\web\JqueryAsset::className()]]);


?>

Just try it. it will work like a charm…

Thanks!!! I understand and it is working fine.