How to disable jQuery autoloading on Ajax request in Yii?

前端 未结 3 1511
野趣味
野趣味 2020-12-14 23:50

I\'m using the following code to generate an ajax request:

echo CHtml::dropDownList(\'teamA\', \'\', EnumController::getTeamOption(), array(
        \'empty\         


        
3条回答
  •  离开以前
    2020-12-15 00:03

    In Yii you should never hardcode any javascript information in the main layout.

    Yii can determine if a client script (javascript) was already included, but for core scripts (like jquery or jqueryui) you have to modify those packages in your config file.

    Open the main.php configuration file and add all the js packages you need within the CClientScript component (you should add it inside components), like this:

    'clientScript'=>array(
      'packages'=>array(
        'jquery'=>array(
          'baseUrl'=>'//ajax.googleapis.com/ajax/libs/jquery/1.8/',
          'js'=>array('jquery.min.js'),
          'coreScriptPosition'=>CClientScript::POS_HEAD
        ),
        'jquery.ui'=>array(
          'baseUrl'=>'//ajax.googleapis.com/ajax/libs/jqueryui/1.8/',
          'js'=>array('jquery-ui.min.js'),
          'depends'=>array('jquery'),
          'coreScriptPosition'=>CClientScript::POS_BEGIN
        )
      ),
    ),
    

    Then, every time you need jquery just add this before your code:

    $cs = Yii::app()->getClientScript();
    $cs->registerCoreScript('jquery');
    

    Yii will then include jquery (or any other script) only once, even if you call it several times in your code.

提交回复
热议问题