Loading old libraries in RequireJS

5

What is the best way to configure old libraries that were not built using the AMD specification? I am referring to libraries that were not defined with the define () statement. In this case, I'm trying to load the JQuery 1.4 library.

I read on the RequireJS site about using the shim attribute, I made the copy of the example in github and executed normally, however I could not make my own example based on the previous one.

I have the following files:

aplicacao/index.html
aplicacao/js/main.js
aplicacao/js/app/app.js
aplicacao/js/lib/require.js
aplicacao/js/lib/jquery-1.4.2.min.js

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>Testando JQuery + RequireJS</title>
        <script data-main="js/main" src="js/lib/require.js"></script>
    </head>
    <body>
        <h1>jQuery 1.4 + RequireJS</h1>
    </body>
</html>

js / main.js

requirejs.config({
    "paths": {
      "jquery": "lib/jquery-1.4.2.min"      
    },
    "shim": {
        "jquery" : "jquery"
    }
});


requirejs(["app/app"], function(app){
    console.log(app); //imprime o objeto definido em js/app/app.js
});

js / app / app.js

define(["jquery"], function(jotaquery) {
    console.log('carregou app ', jotaquery); //não carrega a dependência criada em js/main.js
    console.log(jotaquery); //imprime undefined
    return {
        metodoUm : function() {
          //código aqui
        },
        metodoDois : function() {
          //código aqui 
        }   
    }
});

UPDATED

To make it clearer, here's a simple example to better illustrate my need.

Given the dialogo.js file, implemented without AMD specification (without the use of define ()):

var Dialogo = (function() {
    function exibirAlerta(msg) {
        alert(msg);
    }
    function exibirConfirmacao(msg, callback) {
        if (confirmacao(msg, callback)) {
            if (typeof callback === "function") {
                callback.call();
            }
        }
    }
    return {
        exibirAlerta : exibirAlerta,
        exibirConfirmacao : exibirConfirmacao
    };
})();

How do I load this dependency as it loads the dialogo.js file, but the Dialog variable is not available for manipulation?

require(["dialogo"], function(Dialogo){
    console.log(Dialogo);//undefined
});

If you implement the dialogo.js library according to the AMD standard, as in the code below, the previous statement prints the Dialog object correctly: Object {exibirAlerta: function, exibirConfirmacao: function} .

define(function(){
    function exibirAlerta(msg) {
        alert("DIALOG ALERTA: " + msg);
    }

    function exibirConfirmacao(msg, callback) {
        if (confirmacao(msg, callback)) {
            if (typeof callback === "function") {
                callback.call();
            }
        }
    }

    return {
        exibirAlerta : exibirAlerta,
        exibirConfirmacao : exibirConfirmacao
    };
});
    
asked by anonymous 02.03.2015 / 21:56

1 answer

1

I do not handle much of RequireJS but here's my attempt to help:

Are you sure jQuery JS is not loaded, or is it just the variable that gets undefined? Attempt to enable XmlHttpRequest logging on the browser of your choice, and verify that the JS file is actually being loaded. Also, does a script error occur?

If it does not occur and if the problem is even the variable being undefined, I think the solution is simple, just replace

"shim": {
    "jquery" : "jquery"
}

by

"shim": {
    "jquery" : {
        exports: 'jQuery'
    }
}

Another detail, I had to change 'jquery' to 'jQuery'.

Since jquery 1.4 was not built according to AMD, you should define in shim what is the object to be exported. I took it from here: link

I made a mini-example - gambiarra because I had to play a gist in a separate file and I'm using 2 files instead of 3, but I think it might be useful to show the code working:

link

And the gist (equivalent to app.js):

If this does not help you, let me know in the comments that I change the answer accordingly.

EDIT : I also changed from "define" to "require", I believe this is what you wanted to write, right? Let me know if I'm wrong.

EDIT 2

Fiddle from the example of Dialogo : link

Link to the dialog.js: link

In short, the configuration looks like this:

requirejs.config({
    "paths": {
        "dialogo": "https://cdn.rawgit.com/anonymous/9e1ffe3d76324bd15840/raw/6c5ebe4c1489dd6a484b040384cc36830fdae399/dialogo.js"      
    },
    "shim": {
        dialogo: {
            exports: 'Dialogo'
        }
    }
});

And the code that requires require:

requirejs(["dialogo"], function(Dialogo){
    console.log(Dialogo); //imprime o objeto dialogo
});

See that I have specified the 'Dialog' object in exports. As far as I understand, we have to do this because as js is not in the AMD pattern, it is possible to identify which object should come in return.

If you look at the console you will see that we have the object with its methods:

    

06.03.2015 / 18:03