Symfony检查jQuery是否已包含 [英] Symfony check whether jQuery already included

查看:107
本文介绍了Symfony检查jQuery是否已包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以检查sfContext是否已添加jQuery库, 使用:

Is there any way to check in sfContext whether jQuery library is already added, using:

sfContext::getInstance()->getResponse()->addJavascript('/js/jquery-1.4.2.min.js');

addJavascript('jquery-1.4.2.min.js', sfView::getContext());

use_javascript('jquery-1.4.2.min.js');

在模板中?

在我看来,添加更多jQuery会停止它们的动作.

Seems to me that adding more jQuery's stops their action.

推荐答案

您应该使用自己的支票.

You should use your own check.

  1. 使用您自己的sfWebResponse
  2. 覆盖addJavascript
  3. 添加支票
  1. Use your own sfWebResponse
  2. Override addJavascript
  3. Add the check

因此,使用以下代码在/lib/response/文件夹中创建一个新的myWebResponse.class.php文件(创建它):

So create a new myWebResponse.class.php file in /lib/response/ folder (create it), with this code :

class myWebResponse extends sfWebResponse 
{
  public function addJavascript($file, $position = '', $options = array())
  {
    $this->validatePosition($position);

    // check if the file to add is jquery
    // nb: you might update the regex 
    if (preg_match('/jquery/i', $file))
    {
      // retrieve all loaded js
      $files = array_keys($this->getJavascripts());

      // check if one them containt jquery
      // nb: you might update the regex 
      $isJquery = preg_grep("/jquery/i", $files);

      // jquery found so do not add the file
      if ( ! empty($isJquery))
      {
        return;
      }
    }

    $this->javascripts[$position][$file] = $options;
  }

然后,默认情况下使用您的新回复.在您的apps/frontend/config/factories.yml中,添加:

Then, use your new response by default. In your apps/frontend/config/factories.yml, add:

all:
  response:
    class: myWebResponse 

您完成了.

这篇关于Symfony检查jQuery是否已包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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