Created
February 2, 2012 20:05
-
-
Save mikeobrien/1725456 to your computer and use it in GitHub Desktop.
Making testing of amd modules easier...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.define = function() { | |
var amd = window.amd = window.amd || {}; | |
var scripts = document.getElementsByTagName("script"); | |
var baseUrl; | |
if (!amd.modules) { | |
for (var i = 0; i < scripts.length; i++) { | |
var dataMain = scripts[i].getAttribute('data-base'); | |
if (dataMain) { | |
var a = document.createElement('a'); | |
a.href = dataMain; | |
baseUrl = a.href; | |
break; | |
} | |
} | |
baseUrl = baseUrl || document.URL.substr(0, document.URL.lastIndexOf('/')); | |
if (!/\/$/.test(baseUrl)) baseUrl = baseUrl + '/'; | |
} | |
var script = scripts[scripts.length - 1].src; | |
var modules = amd.modules = amd.modules || { baseUrl: baseUrl}; | |
var constructor = arguments[arguments.length - 1]; | |
module = script.replace(modules.baseUrl, ''); | |
if (/\.js$/.test(script)) module = module.substr(0, module.length - 3); | |
modules[module] = constructor; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['util'], function(util) { | |
return { | |
start: function() { util.log('App starting...'); } | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function() { | |
return { | |
log: function(message) { console.log(message); } | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('app', function() { | |
it('app start should log starting message', function() { | |
var logMessage; | |
var util = { log: function(message) { logMessage = message }}; | |
var app = window.amd.modules['app'](util); | |
app.start(); | |
return expect(logMessage).toBe('App starting...'); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Jasmine Spec Runner</title> | |
<!-- Jasmine stuff... --> | |
<script data-base="../scripts" type="text/javascript" src="amd-modules.js"></script> | |
<script type="text/javascript" src="../scripts/app.js"></script> | |
<script type="text/javascript" src="app-spec.js"></script> | |
<script type="text/javascript"> | |
// Jasmine stuff... | |
</script> | |
</head> | |
<body></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment