Skip to content

Instantly share code, notes, and snippets.

@azder
Last active December 16, 2015 15:09
Show Gist options
  • Save azder/5453932 to your computer and use it in GitHub Desktop.
Save azder/5453932 to your computer and use it in GitHub Desktop.
An idea for creating some kind of declarative require with Require.js
define(['require', 'underscore', 'mylib'], function(require, _, MYLIB) {
// ALWAYS use strict when writing scripts
'use strict';
var modules = {
"name1": "url1",
"name2": "path2",
"name3": "some/other/place/to/load/3",
"name4": "url4"
};
console.log('Loading modules...');
_.each(modules, function(path, name) {
console.log('loading module', name, 'from', path, '...');
MYLIB[name] = require(path);
});
console.log('Framework modules loaded', modules);
});
var config = {
"jquery": null, // it uses "./jquery.js" as the file name
"underscore": "vendor/underscore", // it uses "vendor/underscore.js" as the file name
"$": { "aliasing": "jquery" }, // it creates an alias $ for jquery
"mylib": {
"path": "core/mylib", // it uses "core/mylib.js" as the file name
"deps": [ "jquery", "underscore" ] // it requires jquery and underscore in order, or
"order": false // if false (by default) it loads dependencies out of order
}
}
require(config, function(modules){
// I should be able to access the libraries through
modules.$
modules.underscore
modules.mylib
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment