2013-04-03 36 views
0

這裏加載腳本是我require.js加載文件:與require.js

require.config({ 
    urlArgs: "noCache=" + (new Date).getTime(), 
    paths: { 
     jquery: "vendor/jquery-1.8.2.min", 
     bootstrap: "vendor/bootstrap.min",  
     underscore: "vendor/underscore-min", 
     backbone: "vendor/backbone-min",  
     template: "libs/template"  
    } 
}); 

require(["jquery", "bootstrap", "underscore", "backbone", "template", "main"], 
function ($, bootstrap, underscore, backbone, template, main) { 

})

而且裏面的main.js文件,我有以下代碼:

define(["jquery", "underscore", "backbone"], function ($, _, Backbone) { 
    //Backbone and _ are undefined here, why? 
}) 

那麼爲什麼,_「和,, Backbone」在這裏沒有定義,我做錯了什麼?
如何在其他文件中正確使用它們?

回答

1

如果你是新的骨幹,並要求JS整合來看看下面的教程:

http://backbonetutorials.com/organizing-backbone-using-modules/

您可能正在使用骨幹和突出的非AMD版本。使用墊片的配置可以加載任何庫,甚至非AMD模塊,這裏是從我的源文件中的一個墊片配置片段:

requirejs.config({ 
    baseUrl: 'assets/js/', 
    paths : { 
     'jquery': 'lib/jquery/jquery.min', 
     'underscore': 'lib/underscore/underscore-amd-min', 
     'backbone': 'lib/backbone/backbone-amd-min', 
     'bootstrap': 'lib/bootstrap/bootstrap.min', 
    }, 
    shim: { 
     'backbone': { 
      deps: ['jquery', 'underscore'], 
      exports: 'Backbone' 
     }, 
     'underscore': { 
      exports: '_' 
     }, 
     'bootstrap': { 
      deps: ['jquery'] 
     } 
    } 
}); 

另一種解決辦法使用這些庫的AMD版本你會在這裏找到他們: https://github.com/amdjs

下載並指向您的path部分。

+0

是的...我對這兩個人很陌生,特別是require.js(我只是想第一次整合它)。感謝這些偉大的文章,但您能否更具體地瞭解需要改變的地方? – hjuster

+0

當然,你必須下載amd支持的下劃線版本(https://github.com/amdjs/backbone)和骨幹網(https://github.com/amdjs/backbone),並且包含那些庫而不是當前的庫。 –

+0

@hjuster我只是看着我的源文件,並意識到我也在使用這些庫的非AMD版本。這可以通過shim配置來實現。我編輯了我的答案,幷包含了一個shim配置的例子。 –