在Yii框架上使用ajaxButton / ajaxSubmitButton阻止加载jQuery资产 [英] Prevent loading of jQuery assets with ajaxButton/ajaxSubmitButton on Yii framework

查看:73
本文介绍了在Yii框架上使用ajaxButton / ajaxSubmitButton阻止加载jQuery资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局上有jQuery加载资产,我想使用 CHtml :: ajaxButton / ajaxSubmitButton
但是当我在运行时使用它与另一个渲染时,它再次加载jQuery资产并出错。
如何阻止脚本加载?

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


推荐答案

这也是一个非常讨厌的问题IMO。我解决的方法是创建一个CClientScript派生类,并将其用作clientScript组件(您可以覆盖配置文件中的类类型):

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

我的类重写了registerCoreScript函数,该函数用于注册jii和Yii包含的其他基本脚本。
我在那里做的几乎就是添加ajax的检查,如果是的话,不将它传递给父类。
它看起来像这样:

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);
}

这可以防止在ajax期间加载任何核心脚本(并再次添加资产)调用。

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

您也可以对其他函数执行相同的操作(例如registerScriptFile)

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

希望有帮助。

这篇关于在Yii框架上使用ajaxButton / ajaxSubmitButton阻止加载jQuery资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆