Dar
Dar

Reputation: 401

Prevent loading of jQuery assets with ajaxButton/ajaxSubmitButton on Yii framework

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',
                                        )
                                    );
    ?>

Upvotes: 4

Views: 4992

Answers (3)

sucotronic
sucotronic

Reputation: 1504

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.

Upvotes: 1

Onkar Janwa
Onkar Janwa

Reputation: 3950

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.

Upvotes: 6

Blizz
Blizz

Reputation: 8400

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.

Upvotes: 8

Related Questions