如何在Yii中禁用Ajax请求上的jQuery自动加载? [英] How to disable jQuery autoloading on Ajax request in Yii?
问题描述
我正在使用以下代码来生成ajax请求:
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正在从资产中加载jQuery副本,然后直接从Google加载另一个副本.我只想使用Google副本,并强制Yii不要从资产中加载自己的副本.我该怎么办?
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?
推荐答案
在Yii中,您永远不要在主布局中对任何JavaScript信息进行硬编码.
In Yii you should never hardcode any javascript information in the main layout.
Yii可以确定是否已包含客户端脚本(javascript),但是对于核心脚本(如jquery或jqueryui),您必须在配置文件中修改这些软件包.
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.
打开main.php
配置文件,然后将所需的所有js软件包添加到CClientScript
组件中(应将其添加到components
内部),如下所示:
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
)
),
),
然后,每当您需要jquery时,只需在代码之前将其添加:
Then, every time you need jquery just add this before your code:
$cs = Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');
然后,即使您在代码中多次调用它,Yii也将只包含一次jquery(或任何其他脚本).
Yii will then include jquery (or any other script) only once, even if you call it several times in your code.
这篇关于如何在Yii中禁用Ajax请求上的jQuery自动加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!