2012-05-30 57 views
0

有沒有辦法在sfContext檢查jQuery庫是否已經加入, 使用:Symfony的檢查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停止了他們的行動。

+0

真的嗎?你一定錯了! **我們需要moar jquery !! ** –

回答

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 

大功告成。