Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
642 views
in Technique[技术] by (71.8m points)

php - How to disable jQuery autoloading on Ajax request in Yii?

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

echo CHtml::dropDownList('teamA', '', EnumController::getTeamOption(), array(
        'empty' => '(Team / Single)',
        'ajax' => array(
            'type'=>'POST',
            'url'=> $url,
            'update'=>"#resultA",
            //'data'=>"js:$('#teamA').hide().fadeIn()" 
        )
    )
);

In my main layout, I have the following:

<?php Yii::app()->clientScript->scriptMap=array('jquery.js'=>false);?>
<?php Yii::app()->clientScript->scriptMap=array('jquery.min.js'=>false);?>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js"></script>

Yii is loading jQuery copy out of assets and then -- another copy, directly from Google. I want to use only Google copy and force Yii to not load own copy from assets. How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...