Requring script files from Google and mapping js files to single file?

Well, I’ve been scratching my head for a few hours now over configuring CClientScript.

For a project I’m working on, I have many small javascript files for different pages. When all combined, it comes out to about 5kb compressed, so it makes more sense to do what is outlined here (http://www.yiiframework.com/doc/guide/topics.performance) and combine them all into an “all.js” file.

I tried what is outlined in the performance guide, and can’t seem to get it to work. Not only are jQuery and the like being included from the /assets folder rather than Google, the other files aren’t mapping to /js/all.js.

Here’s what my config looks like:




       'components'=>array(

                ...

                'scriptMap'=>array(

                    'jquery.js'=>false,

                    'jquery.min.js'=>false,

                    'jquery.ajaxqueue.js'=>false,

                    'jquery.metadata.js'=>false,

                    'taskInterface.js'=>'/js/all.js',

                    'ajaxGridButtonAction.js'=>'/js/all.js',

                    'gridViewTooltip.js'=>'/js/all.js',

                    'jquery.tools.min.js'=>'/js/all.js',

                    'project.js'=>'/js/all.js',

                    'projectView.js'=>'/js/all.js',

                ),

                ...

          ),



and here’s what I have at the top of my layouts:




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

        <?php echo CHtml::script(

        CGoogleApi::load('jquery','1.4.2') . "\n" .

        CGoogleApi::load('jquery.ajaxqueue.js') . "\n" .

        CGoogleApi::load('jquery.metadata.js') . "\n" .

        CGoogleApi::load("jqueryui", "1.8.2")

        ); ?>



I’m using some widgets, which use things like:




if($this->jTools===null) {                 $jsfile=dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'js'.DIRECTORY_SEPARATOR.'jquery.tools.min.js';

$this->jTools=Yii::app()->getAssetManager()->publish($jsfile);

}


$cs->registerScriptFile($this->jTools);



and it seems as if those at least should be remapped by CClientScript, but they don’t appear to be.

In some of my views, I’m also using things such as:




echo CHtml::scriptFile(Yii::app()->request->baseUrl.'/js/jquery.tools.min.js');

echo CHtml::scriptFile(Yii::app()->request->baseUrl.'/js/gridViewTooltip.js');



to require my javascript files; I’m not sure if that would work with CClientScript remapping them to /js/all.js. Still, if I use something CClientScript to register a file in the place of CHtml::scriptFile(…), nothing is remapped to /js/all.js.

I need the remapping, because while things work fine on my local machine, the application running live on our server is taking about 400-500 ms to load each javascript file from assets, adding up to quite a few seconds for each page load. If they’re combined, and being loaded from Google, my hope is that the page load times will be slightly more manageable.

One other thing - I’m using quite a bit of ajax and $this->renderPartial(‘someView’,array(‘someParams’=>$someParams),true,false); to render pages and views. Could this be upsetting CClientScript’s expected behavior in some way?

Thanks :)

Did you try:


        'clientScript'=>array(

            'coreScriptUrl'=>'dummy value', // required to prevent asset publishing of core scripts

            'scriptMap'=>array(

                '*.js'=>'all.js',



I’ve also configured the coreScriptUrl, as i don’t want Yii to touch the hard disk on every request.

Thanks for the reply - your response just made me embarrassingly realize that I forgot to put the ‘clientScript’=>array( before specifying the scriptMap options <_< It looks like it’s working now; at least the files aren’t being included from assets, now it’s just a matter of figuring out why things like the jQueryUI tabs broke - this:




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

        <?php echo CHtml::script(

        CGoogleApi::load('jquery','1.4.2') . "\n" .

        CGoogleApi::load('jquery.ajaxqueue.js') . "\n" .

        CGoogleApi::load('jquery.metadata.js') . "\n" .

        CGoogleApi::load("jqueryui", "1.8.2")

        ); ?>



seems like it should be requiring it, I’ll keep digging around see what I can find. Thanks for the help!

Hi!

I’m struggling with this same problem. Do I need to create an empty file, all.js and all.css to get this to work or what do I need to do?

My config:




'clientScript'=>array(

  'scriptMap'=>array(

    '*.js'=>'/js/all.js',

    '*.css'=>'/css/all.css',

  ),

),

...



I’m using $clientScript->registerScriptFile(’/js/init.js’, CClientScript::POS_HEAD); in my main layout, but it won’t find the /js/all.js, and if I create the file, it won’t work at all.

I’m not using the google api at all.

Any ideas?

Then please tell us, what you want to do. Your question implies, you don’t understand yet, what scriptMap is good for. So why use it at all? :)

But to answer your question: Yes, you have to create all.js and include all Javascripts you use on your page. There are also extension which auto-compress javascripts and don’t require a scriptMap.

Yeah, I had little knowledge, but now I know how to use it :)

Compressed the javascript with google compiler, and use scriptMap to map all javascript files to the compressed

file. Now it works, so thanks!