0

以下を使用して、jQueryライブラリがすでに追加されているかどうかをsfContextでチェックインする方法はありますか?

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を追加すると、アクションが停止するようです。

4

1 に答える 1

2

独自のチェックを使用する必要があります。

  1. 自分のを使えsfWebResponse
  2. オーバーライドaddJavascript
  3. チェックを追加

したがって、次のコードを使用して、フォルダーに新しいmyWebResponse.class.phpファイルを/lib/response/作成 (作成) します。

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 

あなたは終わった。

于 2012-05-30T10:12:16.147 に答える