2013-05-14 52 views
1

我main.js看看下面require.js加載引導不確定

requirejs.config({ 
//By default load any module IDs from ../js 
baseUrl: '../js', 
paths : { 
    'jquery': 'libs/jquery', 
    'underscore': 'libs/underscore', 
    'backbone': 'libs/backbone', 
    'bootstrap': 'libs/bootstrap' 
}, 
shim: { 
    'jquery': { 
     exports: '$' 
    }, 
    'backbone': { 
     //These script dependencies should be loaded before loading 
     //backbone.js 
     deps: ['jquery', 'underscore'], 
     //Once loaded, use the global 'Backbone' as the 
     //module value. 
     exports: 'Backbone' 
    }, 
    'underscore': { 
     exports: '_' 
    }, 
    'bootstrap': { 
     deps: ['jquery'], 
     exports: 'Bootstrap' 
    } 
} 
}); 
define(
    ['jquery', 'backbone','underscore', 'bootstrap'], 

    function (j, b, u, boot) { 
     console.log('jquery', j); 
     console.log('backbone', b); 
     console.log('underscore',u); 
     console.log('bootstrap', boot); 
    } 
); 

而且我的控制檯形象是這樣的:

enter image description here

當我警惕的X標誌點擊,他們消失。所以,我認爲bootstrap.js加載正確。但是,它說在控制檯中未定義。任何人都可以讓我清楚是bootstrap.js加載正確和安全使用?爲什麼它說未定義,而其餘部分在控制檯中定義的很好。

+1

可能是因爲所有'jquery','backbone'和'underscore'創建(導出)全局變量,而'bootstrap'只是將插件添加到現有的'jquery'對象中,並且不會全局導出任何東西, t在定義回調中接收任何東西。 – Cyclone 2013-05-14 10:28:02

+0

@Cyclone,所以,從技術上講我以後沒有問題的引導庫。 – user2314342 2013-05-14 12:43:01

+0

是的,如果您在頁面上使用了任何引導程序的東西,並且它的工作原理您不應該有:) – Cyclone 2013-05-14 12:44:39

回答

3

由於jquerybackboneunderscore出口全局變量在其他地方使用,而bootstrap將只取existing jquery object並添加插件的對象,因此它不會出口任何東西。

因此,如果您嘗試在define回撥中收到它,理想情況下它將是undefined。如果您在頁面上使用了任何bootstrap component,並且它正在工作,則意味着集成了引導程序。

從requirejs shim docs

對於那些不需要導出的任何模塊的值只是jQuery的或骨幹插件「模塊」,墊片配置可以只是依賴關係的數組:

requirejs.config({ 
    shim: { 
    'bootstrap': ['jquery'] 
    } 
}); 

所以,如果你願意,你可以聲明自己喜歡它的文檔中指定的引導。