Skip to content

Commit

Permalink
stop failing if dev includes 'curl' as the new apiName or 'define' as…
Browse files Browse the repository at this point in the history
… the new defineName.

also added some more tests to test that configuring apiName or defineName after loading curl works as expected.
  • Loading branch information
John Hann committed Jul 16, 2012
1 parent 497779a commit 9964b1e
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 58 deletions.
35 changes: 19 additions & 16 deletions src/curl.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var window;
"use strict";
var
version = '0.6.4',
userCfg = global['curl'],
curlName = 'curl',
userCfg = global[curlName],
prevCurl,
prevDefine,
doc = global.document,
Expand Down Expand Up @@ -358,40 +359,42 @@ var window;
},

config: function (cfg) {
var hasCfg,
var hasCfg, defineName,
apiName, apiObj, defName, defObj, define;

hasCfg = cfg;

defineName = 'define';

if (!cfg) cfg = {};

// allow dev to rename/relocate curl() to another object
apiName = cfg['apiName'];
apiObj = cfg['apiContext'];
apiName = cfg['apiName'] || curlName;
apiObj = cfg['apiContext'] || global;
// if the dev is overwriting an existing curl()
if (apiObj || apiName ? (apiObj || global)[apiName] : prevCurl && hasCfg) {
throw new Error((apiName || 'curl') + ' already exists');
if (apiObj != global || apiName != curlName ? apiObj[apiName] : prevCurl && hasCfg) {
throw new Error(apiName + ' already exists');
}
(apiObj || global)[apiName || 'curl'] = _curl;
apiObj[apiName] = _curl;
// restore previous curl
if (prevCurl && hasCfg) global['curl'] = prevCurl;
if (prevCurl && hasCfg) global[curlName] = prevCurl;

// allow dev to rename/relocate define() to another object
defName = cfg['defineName'];
defObj = cfg['defineContext'];
if (defObj || defName ? (defObj || global)[defName] : prevDefine && hasCfg) {
throw new Error((defName|| 'define') + ' already exists');
defName = cfg['defineName'] || defineName;
defObj = cfg['defineContext'] || global;
if (defObj != global || defName != defineName ? defObj[defName] : prevDefine && hasCfg) {
throw new Error(defName + ' already exists');
}
(defObj || global)[defName || 'define'] = define = function () {
defObj[defName] = define = function () {
// wrap inner _define so it can be replaced without losing define.amd
var args = core.fixArgs(arguments);
_define(args);
};
// restore previous define
if (prevDefine && hasCfg) global['define'] = prevDefine;
if (prevDefine && hasCfg) global[defineName] = prevDefine;

// indicate our capabilities:
define['amd'] = { 'plugins': true, 'jQuery': true, 'curl': version };
define['amd'] = { 'plugins': true, 'jQuery': true, curlName: version };

// switch to re-runnable config
if (hasCfg) core.config = core.moreConfig;
Expand Down Expand Up @@ -1068,7 +1071,7 @@ var window;
core.checkPreloads(userCfg);

// allow curl to be a dependency
cache['curl'] = _curl;
cache[curlName] = _curl;

// expose curl core for special plugins and modules
// Note: core overrides will only work in either of two scenarios:
Expand Down
43 changes: 43 additions & 0 deletions test/apiAfter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE HTML>
<html>
<head>
<title>apiName/apiContext curl test file</title>

<script>

// simulate existing curl
function existingCurl () {}
curl = existingCurl;

</script>
<script src="../src/curl.js" type="text/javascript"></script>

<script type="text/javascript">

var foo = {},
cfg = {
apiName: 'bar',
apiContext: foo,
paths: {
curl: '../src/curl/'
}
};
curl(
cfg,
[ 'stuff/three', 'domReady!' ]
).then(
function (three) {
var restoredPrev, createdNew;
restoredPrev = curl == existingCurl ? 'SUCCESS' : 'FAILED';
createdNew = foo && foo.bar && typeof foo.bar == 'function' && foo.bar.version ? 'SUCCESS' : 'FAILED';
document.body.appendChild(document.createElement('div')).innerHTML = restoredPrev + ' previous global curl restored';
document.body.appendChild(document.createElement('div')).innerHTML = createdNew + ' new custom curl created';
}
);

</script>

</head>
<body>
</body>
</html>
2 changes: 1 addition & 1 deletion test/apiConflict.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
write('FAILED: curl should not overwrite global curl');
}
catch (ex) {
write('SUCCESS: curl should threw when it conflicted with no recourse: ' + ex.message);
write('SUCCESS: curl should throw when it conflicts with no recourse: ' + ex.message);
}

curl({
Expand Down
40 changes: 20 additions & 20 deletions test/apiName.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<title>apiName/apiContext curl test file</title>

<script>
foo = {};
foo = {};

curl = {
apiName: 'bar',
apiContext: foo,
apiName: 'bar',
apiContext: foo,
paths: {
curl: '../src/curl/'
}
Expand All @@ -19,23 +19,23 @@

<script type="text/javascript">

var contextifiedCurl = foo.bar;

if(typeof contextifiedCurl !== 'function') {
alert('api context foo.bar failed');
}
else {
contextifiedCurl(
[
'stuff/three',
'domReady!'
]
).then(
function (three) {
document.body.appendChild(document.createTextNode('api context foo.bar SUCCESS'));
}
);
}
var contextifiedCurl = foo.bar;

if(typeof contextifiedCurl !== 'function') {
alert('api context foo.bar failed');
}
else {
contextifiedCurl(
[
'stuff/three',
'domReady!'
]
).then(
function (three) {
document.body.appendChild(document.createTextNode('api context foo.bar SUCCESS'));
}
);
}

</script>

Expand Down
43 changes: 22 additions & 21 deletions test/apiName2.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<title>apiName/apiContext curl test file</title>

<script>
foo = {};
foo = {};

curl = {
apiName: 'bar',
apiName: 'require',
paths: {
curl: '../src/curl/'
}
Expand All @@ -16,29 +16,30 @@
</script>
<script src="../src/curl.js" type="text/javascript"></script>

</head>
<body>

<script type="text/javascript">

var contextifiedCurl = window.bar;

if(typeof contextifiedCurl !== 'function') {
alert('api context foo.bar failed');
}
else {
contextifiedCurl(
[
'stuff/three',
'domReady!'
]
).then(
function (three) {
document.body.appendChild(document.createTextNode('api context foo.bar SUCCESS'));
}
);
}
//var contextifiedCurl = require;

if(typeof require !== 'function') {
alert('api context foo.bar failed');
}
else {
require(
[
'stuff/three',
'domReady!'
]
).then(
function (three) {
document.body.appendChild(document.createTextNode('api context is foo.bar SUCCESS'));
}
);
}

</script>

</head>
<body>
</body>
</html>
45 changes: 45 additions & 0 deletions test/defineAfter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE HTML>
<html>
<head>
<title>apiName/apiContext curl test file</title>

<script>

// simulate existing define
function existingDefine () {}
define = existingDefine;

</script>
<script src="../src/curl.js" type="text/javascript"></script>

</head>

<body>
</body>

<script type="text/javascript">

var foo = {},
cfg = {
defineName: 'bar',
defineContext: foo,
paths: {
curl: '../src/curl/'
}
};
curl(
cfg,
[ 'support/foo-bar-define/simple' ]
).then(
function (three) {
var restoredPrev, createdNew;
restoredPrev = define == existingDefine ? 'SUCCESS' : 'FAILED';
createdNew = foo && foo.bar && foo.bar.amd ? 'SUCCESS' : 'FAILED';
document.body.appendChild(document.createElement('div')).innerHTML = restoredPrev + ' previous global define() restored';
document.body.appendChild(document.createElement('div')).innerHTML = createdNew + ' new custom define() created';
}
);

</script>

</html>
5 changes: 5 additions & 0 deletions test/support/foo-bar-define/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foo.bar([], function () {

return 1;

});

0 comments on commit 9964b1e

Please sign in to comment.