in harness/curl.js [454:534]
setApi: function (cfg) {
/*
scenarios:
1. global config sets apiName: "require"
- first call to config sets api
- second and later calls are ignored
- prevCurl cannot exist
2. no global config, first call to config() sets api
- first call to config has no api info
- second call to config sets api
- third and later calls must be ignored
3. global config that doesn't set api, first call does though
- same as #2
4. api info is never set
- how to know when to stop ignoring?
objectives:
1. fail before mistakenly overwriting global[curlName]
2. allow rename/relocate of curl() and define()
3. restore curl() if we overwrote it
*/
var apiName, defName, apiObj, defObj,
failMsg, okToOverwrite;
apiName = curlName;
defName = defineName;
apiObj = defObj = global;
failMsg = ' already exists';
// if we're not setting defaults
if (cfg) {
// is it ok to overwrite existing api functions?
okToOverwrite = cfg['overwriteApi'] || cfg.overwriteApi;
// allow dev to rename/relocate curl() to another object
apiName = cfg['apiName'] || cfg.apiName || apiName;
apiObj = cfg['apiContext'] || cfg.apiContext || apiObj;
// define() too
defName = cfg['defineName'] || cfg.defineName || defName;
defObj = cfg['defineContext'] || cfg.defineContext || defObj;
// curl() already existed, restore it if this is not a
// setDefaults pass. dev must be a good citizen and set
// apiName/apiContext (see below).
if (prevCurl && isType(prevCurl, 'Function')) {
// restore previous curl()
global[curlName] = prevCurl;
}
prevCurl = null; // don't check ever again
// ditto for define()
if (prevDefine && isType(prevDefine, 'Function')) {
// restore previous curl()
global[defineName] = prevDefine;
}
prevDefine = null; // don't check ever again
// check if we're mistakenly overwriting either api
// if we're configuring, and there's a curl(), and it's not
// ours -- and we're not explicitly overwriting -- throw!
// Note: if we're setting defaults, we *must* overwrite curl
// so that dev can configure it. This is no different than
// noConflict()-type methods.
if (!okToOverwrite) {
if (apiObj[apiName] && apiObj[apiName] != _curl) {
throw new Error(apiName + failMsg);
}
// check if we're overwriting amd api
if (defObj[defName] && defObj[defName] != define) {
throw new Error(defName + failMsg);
}
}
}
// set curl api
apiObj[apiName] = _curl;
// set AMD public api: define()
defObj[defName] = define;
},