Skip to content

Instantly share code, notes, and snippets.

@iaincarsberg
Forked from tbranyen/use.js
Created January 16, 2012 17:46
Show Gist options
  • Save iaincarsberg/1621990 to your computer and use it in GitHub Desktop.
Save iaincarsberg/1621990 to your computer and use it in GitHub Desktop.
A RequireJS compatible plugin to provide shimming capabilities declaratively.
/* RequireJS Use Plugin v0.1.0
* Copyright 2012, Tim Branyen (@tbranyen)
* use.js may be freely distributed under the MIT license.
*/
define({
version: "0.1.0",
// Invoked by the AMD builder, passed the path to resolve, the require
// function, done callback, and the configuration options.
//
// Configuration format
// --------------------------------------------------------------------------
//
// The string property used in attach will resolve to window[stringProp]
// Functions are evaluated in the scope of the window and passed all
// arguments.
//
// require.config({
// use: {
// "libs/underscore": {
// attach: "_"
// },
//
// "libs/backbone": {
// deps: ["use!underscore", "jquery"],
// attach: function(_, $) {
// return this.Backbone.noConflict();
// }
// }
// }
// });
//
// Alternativly
// --------------------------------------------------------------------------
//
// You can parse a json string to the use! function, which contains a
// module specfic version of the above config, for example:
//
// define(
// [
// 'use!["libs/underscore", {"attach": "_"}]',
// 'use!["jquery", {"attach": "$"}]'
// ]
// )
//
// Unfortunatly with this solution, the "libs/backbone" implementation isn't
// possible, due to the use of JSON.
//
load: function(name, req, load, config) {
var module = config.use && config.use[name];
// No module to load so return early.
if (!module) {
try {
// Default use to an array.
config.use = config.use || {};
// Parse the json into the use object
var data = JSON.parse(name);
// Makesure the parsed json is in the format we need.
if (
typeof data === "object" &&
data.length === 2 &&
typeof data[0] === 'string' &&
typeof data[1] === 'object'
) {
// If it is, then makesure the used value hasn't already been set
if (config.use[data[0]] === undefined) {
config.use[data[0]] = data[1];
}
// Then require the desired object, and return it in the original
// request load call.
return req(['use!' + data[0]], function (used) {
load(used);
});
}
} catch (e) {
return load();
}
}
// Read the current module configuration for any dependencies that are
// required to run this particular non-AMD module.
req(module.deps || [], function() {
// Require this module
req([name], function() {
// Attach property
var attach = config.use[name].attach;
// If doing a build don't care about loading
if (config.isBuild) {
return load();
}
// Return the correct attached object
if (typeof attach == "function") {
return load(attach.apply(window, arguments));
}
// Use window for now (maybe this?)
return load(window[attach]);
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment