Prevent loading of jQuery assets with ajaxButton/a

2020-06-24 06:04发布

问题:

I have jQuery load asset on my layout and I want to use CHtml::ajaxButton/ajaxSubmitButton. But when I'm using it with another render on runtime it's loading jQuery assets again and makes an error. How to prevent the script from loading?

<?php echo CHtml::ajaxSubmitButton( 'Submit',
                                        CHtml::normalizeUrl(array('site/AjaxRequest/30')),
                                        array(
                                        'error'=>'js:function(){
                                            alert(\'error\');
                                        }',
                                        'beforeSend'=>'js:function(){
                                            alert(\'beforeSend\');
                                        }',
                                        'success'=>'js:function(data){
                                            alert(\'data from server: \'+data);
                                        }',
                                        'complete'=>'js:function(){
                                            alert(\'complete\');
                                        }',
                                        //'update'=>'#response',
                                        )
                                    );
    ?>

回答1:

This is a pretty annoying issue IMO as well. The way I solved is to create a derived class of CClientScript and use that as the clientScript component (you can override the class type in your config file):

'clientScript' => array
(
    'class'                       => 'MyClientScript',
),

My class overrides the "registerCoreScript" function, which is used for registering jQuery and other basic scripts that Yii includes. Pretty much what I do in there is add a check for ajax and not pass it on to the parent class if it is. It looks something like this:

public function registerCoreScript($sName)
{
   if (Yii::app()->request->isAjaxRequest)
      return $this;

   return parent::registerCoreScript($sName);
}

This prevents any core script being loaded (and adding assets again) during an ajax call.

You can do the same for other functions in there too (eg registerScriptFile)

Hope that helps.



回答2:

Yii::app()->clientScript->scriptMap=array(
                    'jquery.js'=>false,
                    'jquery.ba-bbq.js'=>false,
                    'jquery.yiilistview.js'=>false
                ); 

Use this way to prevent loading javascript files which already loaded.



回答3:

Here is another way to circumvent this annoying issue: disable the scripts before de 'render' call:

    Yii::app()->clientScript->corePackages = array();
    Yii::app()->clientScript->scriptMap = array('jquery.yiigridview.js' => false,
                                                'CGridView.css' => false,
                                                'pager.css' => false,
                                                ...
                                                'jquery-ui-i18n.min.js'=> false,);
    echo $this->renderPartial('myView',array('model'=>$model, 'dataProvider'=>$dataProvider),true,true);

You've to put by hand any .js and .css file that you don't want to be inserted in the html. This allows you to enable and disable whatever you want/need.