2014-04-03 80 views
3

我想使用Bootbox與RequireJS定義,但每一次出現這樣的錯誤:Bootbox不RequireJS

ReferenceError: bootbox is not defined

我配置RequireJS與依賴關係:

var 
bowerLocal = "../../../bower_components/", 
asstsLocal = "../../assets/"; 

requirejs.config({ 
    paths: { 
     "jquery": bowerLocal + "jquery/dist/jquery.min", 
     "jquery.bootbox": bowerLocal + "bootbox/bootbox", 
     "jquery.bootstrap": bowerLocal + "bootstrap/dist/js/bootstrap.min", 
     "jquery.elevateZoom": bowerLocal + "elevatezoom/jquery.elevateZoom-2.2.3.min", 
     "jquery.maskedInput": bowerLocal + "jquery-maskedinput/dist/jquery.maskedinput.min" 
    }, 
    /** 
    * Define as dependências de bibliotecas. 
    */ 
    shim: { 
     "jquery.bootstrap": { 
      deps: ["jquery"] 
     }, 
     "jquery.elevateZoom": { 
      deps: ["jquery"] 
     }, 
     "jquery.maskedInput": { 
      deps: ["jquery"] 
     }, 
     "jquery.bootbox": { 
      deps: ["jquery", "jquery.bootstrap"], 
      exports: 'bootbox' 
     } 
    } 
}); 

require(
    ["jquery", "jquery.bootstrap", "jquery.bootbox", "jquery.maskedInput", "jquery.elevateZoom"], 
    function (util) { 

bootbox.alert("Hi"); 
} 
... 

回答

1

bootbox不在AMD模式下的窗口中定義,你必須自己定義它,所以在完成下面的工作後,它現在可以工作。

define(["bootbox"], function(bootbox){ 
bootbox.alert("Hello Bootbox"); 
}); 

當然,路徑定義在require.config(),這將工作。

1

我覺得@ Pushker的回答他輸入define,他其實是想鍵入require

require(["bootbox"], function(bootbox) { bootbox.alert("Hello Bootbox"); });

(他也輸錯一個}{

此外,具有多個依賴時您還應該在require中指定這些參數作爲函數的參數,並確保它們與字符串數組參數中的順序相同,例如:

require(["jquery", "jquery.bootstrap", "jquery.bootbox", ...], function(jquery, bootstrap, bootbox, ...) { bootbox.alert("Hi"); } ... });

檢查我搗鼓工作Bootbox例如:http://jsfiddle.net/bartvanderwal/L1emr4up/