I got some AMD/CommonJS adapter code from here: supporting both CommonJS and AMD
(function (name, definition) {
if (typeof module != 'undefined') {
module.exports = definition();
}
else if (typeof define == 'function' && typeof define.amd == 'object') {
define(name, [], definition);
}
else {
this[name] = definition();
}
}('modXyz', {
sayHi:function (name) {
console.log('Hi ' + name + '!');
}
}
));
I'd like to use that code with Curl to make all my code AMD/CommonJS compatible. What I was expecting to be able to do was this:
greeter = curl(['modXyz']);
greeter.sayHi("Gracie");
But the object that curl
returns isn't the object I'm expecting. The closest I can get is this:
curl(['modXyz'], function(mod) { window.greeter = mod; });
greeter.sayHi("Gracie");
Which seems to defeat the purpose of AMD. Is curl capable of doing something like this? Do I have to use require.js to get it to happen?