Tom
Tom

Reputation: 769

Bootbox don't defined with RequireJS

I am trying to use Bootbox with RequireJS, but every time this error appears:

ReferenceError: bootbox is not defined

I configured RequireJS with the dependencies:

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");
}
...

Upvotes: 3

Views: 11926

Answers (2)

Pushker Yadav
Pushker Yadav

Reputation: 856

bootbox is not defined on window in AMD mode, you have to define it yourself, so after doing the below it works now.

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

Of course the path is defined in require.config(), This will work.

Upvotes: 1

Bart
Bart

Reputation: 5203

I think in @Pushker's answer he typed define where he actually wanted to type require:

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

(he also mistyped a } for a {.

Furthermore, when having multiple dependencies you should also specify these as parameter's of the function within your require AND make sure they are in the same order as in the string array parameter. E.g. like so:

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

Check my fiddle for a working Bootbox example: http://jsfiddle.net/bartvanderwal/L1emr4up/

Upvotes: 1

Related Questions