Yii2: Should remove the JqueryAsset registration from [yii\web\View::registerJs()] ?

Created on 23 Sep 2016  路  9Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

If I have to use JqueryAsset implemented by myself for a specified version , the yii\web\View::registerJs() method will register the built-in yii\web\JqueryAsset automatically ! Then I have two jquery.js in one Page ~~~~

ps:

  1. I have to use Jquery for a specified version , because some jquery plugins required.
  2. I always want to use yii\web\View::registerJs() as well.
  3. Now, I HAVE TO configure the assetsManager to solve my problem , it doesn't look smart.

What is the expected result?

Just remove it !
Do not use JqueryAsset in yii\web\View::registerJs() to deal with the yii\web\View::POS_READY things.
Alternatively , just use NATIVE javascript instead.

What do you get instead?

NATIVE javascript

Additional info

Also consider to remove [yii\web\Jquery] from yii2 ?

| Q | A |
| --- | --- |
| Yii version | 2.0.9 |
| PHP version | PHP 7.0 |
| Operating system | OS X |

JS question

Most helpful comment

In general jQuery $(document).ready() statement can be replaced by pure JavaScript analog:

document.addEventListener("DOMContentLoaded", function(event) { 
  //do work
});

All 9 comments

Use Dependecy Injection Container and override yii\web\JqueryAsset with your own.

Yii::$container->set('yii\web\JqueryAsset', 'you\own\JqueryAsset');

See Dependeny injection container guide page to know more.

_This is an automated comment, triggered by adding the label question._

Please note, that the GitHub Issue Tracker is for bug reports and feature requests only.

We are happy to help you on the support forum, on IRC (#yii on freenode), or Gitter.

Please use one of the above mentioned resources to discuss the problem.
If the result of the discussion turns out that there really is a bug in the framework, feel free to
come back and provide information on how to reproduce the issue. This issue will be closed for now.

Or even easier - override bundles array

http://www.yiiframework.com/doc-2.0/yii-web-assetmanager.html

I know the two ways you said above, but what I want to say is not that.
For example , If I use View::registerJs() , but I don't want to use Jquery in my page. Maybe I can't use it because of my BOSS , my Leader etc.
This is what I want to express.
Why hard-coding the Jquery in the View class?

@SilverFire

JqueryAsset is loaded only in case you are using View::POS_READY or View::POS_LOAD position for your JavaScript files. It is loaded because these positions require JQuery functions to be actually applied.

If you do not want JQuery o be involved in the process you should use other script positions.

Thank you

In general jQuery $(document).ready() statement can be replaced by pure JavaScript analog:

document.addEventListener("DOMContentLoaded", function(event) { 
  //do work
});

Resolved via #14865

Was this page helpful?
0 / 5 - 0 ratings