Skip to content

Instantly share code, notes, and snippets.

@jboesch
Created October 22, 2010 22:35
Show Gist options
  • Save jboesch/641505 to your computer and use it in GitHub Desktop.
Save jboesch/641505 to your computer and use it in GitHub Desktop.
requireJS dependencies
// Using http://requirejs.org/
// loading scripts/misc/person.js
require(["misc/person"], function(person) {
person.loaded(function(){
alert('dependencies loaded!');
// dependencies loaded, now do something with person
});
});
// Inside scripts/misc/person.js
define(function(){
var dependencies = [
'helper/ear', // looking to scripts/helper/ear.js
'helper/nose',
'helper/eyes'
];
return {
loaded: function(cb){
require(dependencies, function(){
cb.apply(this, arguments);
});
}
}
});
@SlexAxton
Copy link

should that be require.def ?

@jboesch
Copy link
Author

jboesch commented Oct 22, 2010

Apparently it's recently been changed - http://requirejs.org/docs/api.html:

"NOTE: As of RequireJS 0.14.3, the function define() is preferred to create modules. Previously it was require.def(). require.def is still available, but define() is encouraged in the interests of conforming to the Asynchronous Module Proposal. You are free to continue require.def() if you only want RequireJS to load the modules, but if you want your code to be potentially interoperable with other Async Module script loaders, you should consider using define(). Any API examples that use define() will work the same if require.def() is used instead."

..I don't really plan on using this code with any other Async Module script loaders... just thought I'd do it if it's preferred :/

@SlexAxton
Copy link

oh, good to know. Doesn't it take a string as the first argument, or are you using the file location to define the module name?

@jboesch
Copy link
Author

jboesch commented Oct 22, 2010

Yeah I'm just using the file location as the module name. I figured I'd just save some chars

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment