Question

I'm building a project in JavaScript, and I'd like to respect the AMD convention, but I need my project to be available from the global environment (just by adding it's name).

Dojo is the perfect example since it's exactly what I want : valid AMD, but Dojo is available from the global env. I tried to look at their source, but didn't find where they define Dojo.

So far, the only solution I came up with is this :

project.js :

var Project = {
    // some stuffs in there
};

module.js :

require(['project'], function () {
    Project.Module = {
        // some stuffs in there
    };
});

I know it's not the best way to do it, and I believe module.js should return {}, not define it.

What is the best way?

Was it helpful?

Solution

I think a good option would be to check for define and if it is available define your project as a module.

if ( typeof define === "function"){
  define('project', [], function(){ return project; })
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top