2012-06-24 48 views
4

我試圖通過Requirejs模塊化我的Backbone應用程序,但我似乎無法得到Requirejs來包含Backbone。這是我main.js是被從索引頁包括:無法得到requirejs來包含Backbonejs

require.config({ 
    baseUrl: '/static/js/', 
    paths: { 
     jquery: 'libs/jquery/jquery-min', 
     underscore: 'libs/underscore/underscore-min', 
     backbone: 'libs/backbone/backbone-min', 
     text: 'libs/require/text', 
    }, 
}); 

require(['/static/js/views/app.js'], function(AppView) { 
    var appView = new AppView; 
}); 

這裏是我的app.js,但未通過上面的文件包括:

define([ 
    'jquery', 
    'underscore', 
    'backbone', 
], function($, _, Backbone) { 
    console.log($); 
    console.log(_); 
    console.log("inside of the app js file"); 
    var AppView = Backbone.View.extend({ 

     initialize: function() { 
      console.log("inside of appview initialize"); 
     }, 

    }); 
}); 

以下信息會在打印出我的谷歌Chrome控制檯:

function (a,b){return new e.fn.init(a,b,h)} 
app.js:7undefined 
app.js:8inside of the app js file 
app.js:9Uncaught TypeError: Cannot read property 'View' of undefined 

$ works的定義,但_和Backbone的定義不起作用。他們沒有定義。任何想法爲什麼發生這種情況?

回答

5

我建議你使用requireJS的版本具有的jQuery捆綁在一起。它會爲你節省很多設置痛苦。你可以在這裏閱讀細節:http://requirejs.org/docs/jquery.html這裏下載文件:https://github.com/jrburke/require-jquery

在他們自己的話說:

隨着RequireJS,腳本可以以不同的順序比他們指定的順序加載。這可能會導致假設jQuery已被加載的jQuery插件出現問題。使用合併的RequireJS + jQUery文件可以確保jQuery在加載任何jQuery插件之前處於頁面中。

這應該採取與requireJS正確加載的jQuery的護理:

<script data-main="js/main" src="js/require-jquery.js"></script> 

Main.js

有兩點要注意這裏:

  • 無需重新定義jquery的路徑,因爲這已經照顧了
  • 你仍然需要指出的jQuery爲必需的模塊
  • (我不得不更新的路徑,讓他們在我的系統工作)

代碼:

require.config({ 
    baseUrl: 'js/', 
    paths: { 
     underscore: 'libs/underscore-min', 
     backbone: 'libs/backbone-min', 
    }, 
}); 

require(['jquery', 'app'], function($, AppView) { 
    console.log("done w/ requires"); 
    console.log($) 
    console.log(AppView) 
    var appView = new AppView; 
}); 

app.js

一對夫婦的注意事項:

  • 如果JS文件已被定義爲模塊,則只能在回調中加載JS文件。所以函數($,_,Backbone)將會失敗。
  • 你必須返回你的對象,以便它可以在main.js回調使用(返回APPVIEW)

代碼:

define(
    [ 
    'jquery', 
    'underscore', 
    'backbone', 
    ], 
    function() { 
     console.log($); 
     console.log(_); 
     console.log(Backbone); 
     console.log("inside of the app js file"); 
     return AppView = Backbone.View.extend({ 
      initialize: function() { 
      console.log("inside of appview initialize"); 
     }, 
    }); 
}); 

控制檯

與該代碼在地點,這是控制檯輸出:

function (selector, context) { 
    // The jQuery object is actually just the init constructor 'enhanced' 
    return new jQuery.fn.init(selector, context, rootjQuery); 
    } [app.js:8] 

function (a){return new m(a)} [app.js:9] 

Object [app.js:10] 

inside of the app js file [app.js:11] 

done w/ requires main.js:21 

function (selector, context) { 
    // The jQuery object is actually just the init constructor 'enhanced' 
    return new jQuery.fn.init(selector, context, rootjQuery); 
} [main.js:22] 

function(){a.apply(this,arguments)} [main.js:23] 

inside of appview initialize [app.js:15] 
5

我會迴避使用分叉版本的主幹和下劃線。 requirejs添加了一個「shim」配置選項來處理本機不支持AMD的第三方庫。這使得更新到最新版本的庫更容易。

http://requirejs.org/docs/api.html#config-shim

下面是一個例子:

require.config({ 
    paths: { 
    jquery: "libs/jquery", 
    underscore: "libs/underscore", 
    backbone: "libs/backbone" 
    }, 
    shim: { 
    underscore: { 
     exports: '_' 
    }, 
    backbone: { 
     deps: ["underscore", "jquery"], 
     exports: "Backbone" 
    } 
    } 
}); 
相關問題