2013-04-28 108 views
21

我似乎无法弄清楚如何通过RequireJS加载Bootstrap。没有发现我找到的例子。无法通过RequireJS加载Bootstrap

这里是我的垫片:

require.config({ 
    // Sets the js folder as the base directory for all future relative paths 
    baseUrl: "./js", 
    urlArgs: "bust=" + (new Date()).getTime(), 
    waitSeconds: 200, 
    // 3rd party script alias names (Easier to type "jquery" than "libss/jquery, etc") 
    // probably a good idea to keep version numbers in the file names for updates checking 
    paths: { 

     // Core libsraries 
     // -------------- 
     "jquery": "libs/jquery", 
     "underscore": "libs/lodash", 
     "backbone": "libs/backbone", 
     "marionette": "libs/backbone.marionette", 

     // Plugins 
     // ------- 
     "bootstrap": "libs/plugins/bootstrap", 
     "text": "libs/plugins/text", 
     "responsiveSlides": "libs/plugins/responsiveslides.min", 
     'googlemaps': 'https://maps.googleapis.com/maps/api/js?key=AIzaSyDdqRFLz6trV6FkyjTuEm2k-Q2-MjZOByM&sensor=false', 


     // Application Folders 
     // ------------------- 
     "collections": "app/collections", 
     "models": "app/models", 
     "routers": "app/routers", 
     "templates": "app/templates", 
     "views": "app/views", 
     "layouts": "app/layouts", 
     "configs": "app/config" 

    }, 

    // Sets the configuration for your third party scripts that are not AMD compatible 
    shim: { 

     "responsiveSlides": ["jquery"], 
     "bootstrap": ["jquery"], 
     "backbone": { 

     // Depends on underscore/lodash and jQuery 
     "deps": ["underscore", "jquery"], 

     // Exports the global window.Backbone object 
     "exports": "Backbone" 

     }, 
     "marionette": { 
     // Depends on underscore/lodash and jQuery 
     "deps": ["backbone", "underscore", "jquery"], 
     // Exports the global window.Backbone object 
     "exports": "Marionette" 
     }, 
     'googlemaps': { 'exports': 'GoogleMaps' }, 
     // Backbone.validateAll plugin that depends on Backbone 
     "backbone.validate": ["backbone"] 

    }, 
    enforceDefine: true 

}); 

,这里是我如何调用引导:

define([ 
     "jquery", 
     "underscore", 
     "backbone", 
     "marionette", 

     "collections/Navigations", 
     'bootstrap', 
     ], 
function($, _, Backbone, Marionette, Navigations, Bootstrap){ 

,我得到的错误是这样的:

Uncaught Error: No define call for bootstrap 

如何任何想法解决这个问题?

回答

12

Bootstrap lib不会返回任何对象,如jQuery,Underscore或Backbone:此脚本只是通过添加新方法来修改jQuery对象。所以,如果你想使用的引导库,你只需要在模块中添加和使用jQuery的方法,像往常一样(不declarating引导像PARAM,因为该值undefined):

define([ 
    "jquery", 
    "underscore", 
    "backbone", 
    "marionette", 
    "collections/Navigations", 
    "bootstrap", 
    ], 

function($,_,Backbone,Marionette,Navigations){ 
    $("#blabla").modal("show"); //Show a modal using Bootstrap, for instance 
}); 
+0

谢谢你的帮助在这。在我发布这个问题之前,我尝试了这个,现在我又试了一次 - 它不起作用。页面不会加载,但在控制台中没有错误。任何其他建议感激。 – 2013-05-01 03:42:04

+0

现在我添加了“enforceDefine:true”,并且出现以下错误“No define call for libs/plugins/bootstrap”,但路径是正确的。所以,我真的不知道该从哪里出发。 – 2013-05-01 03:54:09

+0

这个答案http://stackoverflow.com/questions/13377373/shim-twitter-bootstrap-for-requirejs将帮助与enforceDefine错误 – Andrew 2013-05-04 00:10:41

7

我发现这是足以以下内容添加到我的requirejs.config调用(伪):

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

我喜欢用Require.Js ORDER插件,它做什么?简单地装载所有的图书馆,以便在这种情况下,你不会得到任何错误,喔和引导取决于jQuery的,所以我们需要使用垫片在这种情况下:

requirejs.config({ 
    baseUrl: "./assets", 
    paths: { 
     order: '//requirejs.org/docs/release/1.0.5/minified/order', 
     jquery: 'http://code.jquery.com/jquery-2.1.0.min', 
     bootstrap: '//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min' 
    }, 
    shim: { 
     'bootstrap': { 
      deps:['jquery'] 
     } 
    } 
}); 

require(['order!jquery', 'order!bootstrap'], function($) { 

});