【问题标题】:Symfony check whether jQuery already includedSymfony 检查 jQuery 是否已经包含
【发布时间】:2012-05-30 09:53:17
【问题描述】:

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

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 会阻止他们的行动。

【问题讨论】:

  • 真的吗?你一定弄错了! 我们需要 moar jquery !!

标签: php jquery symfony1 symfony-1.4


【解决方案1】:

您应该使用自己的支票。

  1. 使用你自己的sfWebResponse
  2. 覆盖addJavascript
  3. 添加支票

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

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 中,添加:

all:
  response:
    class: myWebResponse 

你已经完成了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-09
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多