9

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?

3 Answers 3

17

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.

Sign up to request clarification or add additional context in comments.

4 Comments

wow! thanks a million...and here i thought i knew something about Yii by now :)
For other JS files (not those defined as 'core scripts' in the config) you can use isScriptFileRegistered() See: yiiframework.com/doc/api/1.1/…
If you use registerScript(File) with POS_READY, you do not have to register jquery manually, it is registered automatically. You have to register jquery-ui manually, however.
this put jquery right at the end of all include scripts. how do you make it at the top?
6

Following the instructions from Yii Special Topics Performance, I made a quick test, and this worked:

At the top of the main layout:

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

In the <head> section of the main layout:

<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>

or

<?php echo CGoogleApi::init(); ?>

<?php echo CHtml::script(
    CGoogleApi::load('jquery','1.7.2') . "\n" .
    CGoogleApi::load('jqueryui','1.8.17') 
); ?>

I would actually use the CGoogleApi helper, but the jsapi apparently cannot load jquery versions higher than those in my example! I guess that's slow updating on Google's part, because it doesn't work in a plain file either.

Or maybe the js file was still in your cache?

3 Comments

thanks! not quite what i was looking for but great to know about CGoogleApi - where did you see it in the documentation?
I have the link included in my answer, just scroll down a little. It's official docs, so I guess there are more ways to do it, although e.a.'s answer is cleaner, because it works with the register function.
CGoogleApi::load('jquery','1.7.2') . "\n" . does not exist. how would you load the latest jquery?
2

This will work

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.