Maxime R. Maxime R. - 7 months ago 19
Javascript Question

Setup smooch-js with RequireJS

I followed Smooch instructions with no success.

Here is the minimal code that fails for me:

<!doctype html><html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js"></script>
<script>
require.config({
"paths": {
"smooch": "https://cdn.smooch.io/smooch.min"
}
});
// Tried this
require(["require", "smooch"], function(require){
var Smooch = require("smooch");
console.log(Smooch); // → undefined
});
// Tried that
require(["smooch"], function(Smooch){
console.log(Smooch); // → undefined
});
</script>
</head>
<body></body>
</html>


require
returns me an
undefined
so no
Smooch.init(...)
for me. Tested in both Firefox and Chrome.

Am I doing something wrong ?

Answer

You should use "Smooch" instead of "smooch" in your require calls, like this :

<!doctype html><html>
<head>
    <meta charset="utf-8">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js"></script>
    <script>
        require.config({
            "paths": {
                "Smooch": "https://cdn.smooch.io/smooch.min"
            }
        });
        // Tried this
        require(["require", "Smooch"], function(require){
            var Smooch = require("Smooch");
            console.log(Smooch);  // → Smooch object
        });
        // Tried that
        require(["Smooch"], function(Smooch){
            console.log(Smooch);  // → Smooch object
        });
    </script>
</head>
<body></body>
</html>

The lib is wrapped in a UMD wrapper and self-defines its name to be "Smooch".

Comments