2011-11-28 22 views
0

我需要加載jQuery1.7的模塊,我見過的@jrburke驗證碼:Requirejs不從CDN加載jquery1.7作爲模塊

requirejs.config({ 
    paths: { 
    'jquery' : 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min' 
    } 
}); 

require(['jquery'], function($) { 
    //$ points to jQuery 
}); 

這不是對我來說是非常有用的,因爲所有的.js名稱是由服務器端生成的,我從php-array獲取它們。

所以,我寫了這個:

require(['http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js'], 
     function($) { 
    //$ points to jQuery 
}); 

$爲null,該函數內。

UPDATE

這是我的PHP的模板,使我的JS-腳本此頁:

<script src="http://requirejs.org/docs/release/1.0.1/minified/require.js"> 
</script> 

<script> 
    require([ 
     <?php echo "'". implode("',\n\t'", $this->scripts) . "'\n"; ?> 
    ], function($){ 

     console.warn ($); // null ;(

     // loaded jQuery 
     window.$ = $; 

     // Load main client script for this page 
     boot('<?php echo $this->eprint($this->content_page); ?>'); 

    }); 
</script> 

,這是我的PHP陣列此頁面(index頁):

$scripts = array(
    'http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js', 
    'http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js', 
    '/js/libs/jquery.history.js?v=1321687090', 
    '/js/libs/coolclock.js?v=1321629683', 
    '/js/libs/excanvas.js?v=1321629683', 
    '/js/client.modules.js?v=1321703735', 
    '/js/client.all.js?v=1322512192', 
    '/js/boot.js?v=1322512037', 
    '/js/client.index.js?v=1321689884' 
); 
+0

可能重複(http://stackoverflow.com/questions/8070959/sourcing-jquery-from-a-cdn) – Bevan

回答

1

的形式爲你的PHP數組:

$jquery = array (
    'jQuery' => 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js' 
); 

然後嘗試:

requirejs.config({ 
    paths: <?php echo json_encode($jquery) ?> 
}); 

require(['jquery'], function($) { 
    //$ points to jQuery 
}); 
[?採購的jQuery從CDN]的
+0

我我更新了我的問題。增加'php'代碼 –

+1

@Innuendo只是讓它變成現實,你的生活將會變得更加簡單:-) – Neal

+0

所以我可以在'requirejs.config'的'paths'道具中添加我的數組的每個元素?這很正常,並非所有的都是模塊化的,不是嗎? –