【问题标题】:Stop loading jquery library from assets folder in Yii停止从 Yii 中的 assets 文件夹加载 jquery 库
【发布时间】:2014-12-10 10:17:00
【问题描述】:

在检查了萤火虫后,我的应用程序出现了问题,导致它变慢了 我注意到 jquery-ui 第一次从 google.com 加载两次,第二次从 assets 文件夹 ("232kb") 加载。

如何强制它从没有资产版本的 google.com 加载?

查看内容“JavaScript调用Ajax函数”:

....
     $(".third,#second-next,#fourth-pr").click(function () {

                $.ajax({
                    url: '<?php echo Yii::app()->createUrl('site/CallScientificForm',array('language'=>language())); ?>',
                    type: 'GET',
                    dataType: 'html',
                    beforeSend: function () {
                        $("#loading").show();
                    },
                    success: function (data, textStatus, xhr) {
                        $("#hr3").css("background", "#51a351");
                        $("*").removeClass("active");
                        $(".third").addClass("active");
                        $("#firstContent ,#secondContent,#thirdContent,#fourthContent").fadeOut(2000);
                        $("#thirdContent").html(data);
                        $("#thirdContent").fadeIn(2000);
                        $("#loading").hide();
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        $('#' + id + ' .contentarea').html(textStatus);
                    },
                    complete: function() {
                        $(this).data('requestRunning', false);
                    }
                });

            });
...

控制器:

...
  public function actionCallScientificForm()
    {
        $scienceModel = new MembershipScientific();
        $view= $this->renderPartial('_ScientificForm', array('scienceModel' => $scienceModel, 'language' => language()), false, true);
        echo $view;
        Yii::app()->end();
    }
..

主要配置:

 'clientScript'=>array(
            'packages'=>array(
                'jquery'=>array(
                    'baseUrl'=>'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/',
                    'js'=>array('jquery.js'),
                    'coreScriptPosition'=>CClientScript::POS_END
                ),
                'jqueryMin'=>array(
                    'baseUrl'=>'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/',
                    'js'=>array('jquery.min.js'),
                    'coreScriptPosition'=>CClientScript::POS_END
                ),
                'jquery-ui'=>array(
                    'baseUrl'=>'https://code.jquery.com/ui/1.11.1/jquery-ui.min.js',
                    'js'=>array('jquery-ui.min.js'),
                    'depends'=>array('jquery'),
                    'coreScriptPosition'=>CClientScript::POS_END
                )
            ),
        ),

我在目标视图中这样称呼它:

....
cs = Yii::app()->getClientScript();

$cs->registerCoreScript('jquery');
$cs->registerCoreScript('jquery-ui');
....

【问题讨论】:

    标签: php jquery jquery-ui yii


    【解决方案1】:

    clientScript 中的它们设置为 false 以防止它们加载。

    您可以禁用的几个脚本示例:

    'components' => array(
        'clientScript' => array(
    
            // disable default yii scripts
            'scriptMap' => array(
                'jquery.js'     => false,
                'jquery.min.js' => false,
                'core.css'      => false,
                'styles.css'    => false,
                'pager.css'     => false,
                'default.css'   => false,
            ),
        ),
    

    【讨论】:

      【解决方案2】:

      使用脚本映射:

      'clientScript' => [
          'class' => 'CClientScript',
          'scriptMap' => [
               'jquery.js' => '//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js',
               'jquery.min.js' => '//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js',
               'jquery-ui.js' => '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js',
               'jquery-ui.min.js' => '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js'
          ],
      ....
      

      同时从包中移除 jQuery 和 jQuery ui。这应该可以解决双重注册问题。

      注意:最好使用协议相关的网址://example.com/blah/ - 这种方式也适用于https

      【讨论】:

      • 忘了删除“核心”包,因为这些双核心脚本更新了答案。
      猜你喜欢
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 2011-11-21
      • 1970-01-01
      相关资源
      最近更新 更多