Permalink
Browse files

Fixes #36, permit require errbacks in code, but do not call them.

  • Loading branch information...
1 parent 300c855 commit f7162da5cd5aae236878c9517d477d03cb8f573c @jrburke jrburke committed Aug 19, 2012
Showing with 150 additions and 14 deletions.
  1. +22 −14 almond.js
  2. +1 −0 tests/all.js
  3. +127 −0 tests/errback/errback.html
View
@@ -10,12 +10,12 @@
var requirejs, require, define;
(function (undef) {
- var defined = {},
+ var main, req,
+ defined = {},
waiting = {},
config = {},
defining = {},
- aps = [].slice,
- main, req;
+ aps = [].slice;
/**
* Given a relative module name, like ./something, normalize it to
@@ -26,11 +26,11 @@ var requirejs, require, define;
* @returns {String} normalized name
*/
function normalize(name, baseName) {
- var baseParts = baseName && baseName.split("/"),
+ var nameParts, nameSegment, mapValue, foundMap,
+ foundI, foundStarMap, starI, i, j, part,
+ baseParts = baseName && baseName.split("/"),
map = config.map,
- starMap = (map && map['*']) || {},
- nameParts, nameSegment, mapValue, foundMap,
- foundI, foundStarMap, starI, i, j, part;
+ starMap = (map && map['*']) || {};
//Adjust any relative paths.
if (name && name.charAt(0) === ".") {
@@ -48,7 +48,8 @@ var requirejs, require, define;
name = baseParts.concat(name.split("/"));
//start trimDots
- for (i = 0; (part = name[i]); i++) {
+ for (i = 0; i < name.length; i += 1) {
+ part = name[i];
if (part === ".") {
name.splice(i, 1);
i -= 1;
@@ -201,9 +202,9 @@ var requirejs, require, define;
}
main = function (name, deps, callback, relName) {
- var args = [],
- usingExports,
- cjsModule, depName, ret, map, i;
+ var cjsModule, depName, ret, map, i,
+ args = [],
+ usingExports;
//Use name if no relName
relName = relName || name;
@@ -215,7 +216,7 @@ var requirejs, require, define;
//values to the callback.
//Default to [require, exports, module] if no deps
deps = !deps.length && callback.length ? ['require', 'exports', 'module'] : deps;
- for (i = 0; i < deps.length; i++) {
+ for (i = 0; i < deps.length; i += 1) {
map = makeMap(deps[i], relName);
depName = map.f;
@@ -251,7 +252,7 @@ var requirejs, require, define;
//favor that over return value and exports. After that,
//favor a non-undefined return value over exports use.
if (cjsModule && cjsModule.exports !== undef &&
- cjsModule.exports !== defined[name]) {
+ cjsModule.exports !== defined[name]) {
defined[name] = cjsModule.exports;
} else if (ret !== undef || !usingExports) {
//Use the return value from the function.
@@ -265,7 +266,7 @@ var requirejs, require, define;
}
};
- requirejs = require = req = function (deps, callback, relName, forceSync) {
+ requirejs = require = req = function (deps, callback, relName, forceSync, alt) {
if (typeof deps === "string") {
//Just return the module wanted. In this scenario, the
//deps arg is the module name, and second arg (if passed)
@@ -289,6 +290,13 @@ var requirejs, require, define;
//Support require(['a'])
callback = callback || function () {};
+ //If relName is a function, it is an errback handler,
+ //so remove it.
+ if (typeof relName === 'function') {
+ relName = forceSync;
+ forceSync = alt;
+ }
+
//Simulate async callback;
if (forceSync) {
main(undef, deps, callback, relName);
View
@@ -16,3 +16,4 @@ doh.registerUrl("missing", "../missing/missing.html");
doh.registerUrl("insertRequire", "../insertRequire/insertRequire.html", 4000);
doh.registerUrl("circular", "../circular/circular.html");
doh.registerUrl("relativePaths", "../relativePaths/relativePaths.html");
+doh.registerUrl("errback", "../errback/errback.html");
@@ -0,0 +1,127 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>almond: errback Test</title>
+ <script type="text/javascript" src="../doh/runner.js"></script>
+ <script type="text/javascript" src="../doh/_browserRunner.js"></script>
+ <script type="text/javascript" src="../../almond.js"></script>
+ <script type="text/javascript">
+ var master = new doh.Deferred();
+ count = 0;
+
+ function done() {
+ count += 1;
+ if (count === 2) {
+ master.callback(true);
+ }
+ }
+
+ //Register the test
+ doh.register(
+ "errback",
+ [
+ {
+ name: "errback",
+ timeout: 2000,
+ runTest: function () {
+ return master;
+ }
+ }
+ ]
+ );
+ doh.run();
+
+
+ define('plug', function () {
+ return {
+ load: function (id, require, load, config) {
+ require([id], load, function (err) {
+ throw err;
+ });
+ }
+ }
+ })
+
+ define('c', ['plug!foo'], function (foo) {
+ return {
+ name: 'c',
+ foo: foo
+ };
+ });
+
+ define('fu/a', ['require'], function(require){
+ var myC = null;
+
+ require(['c'], function (c) {
+ myC = c;
+ doh.is('c', myC.name);
+ doh.is('foo', myC.foo.name);
+ done();
+ }, function (err) {
+ //This should not be triggered
+ throw err;
+ });
+
+ //Make sure require is async
+ doh.is(null, myC);
+
+ return {
+ name: 'fu/a'
+ };
+ });
+
+ define('foo', {
+ name: 'foo'
+ });
+
+ define('foo/bar/c',['../../fu/a'], function(v){
+ return {
+ name: 'foo/bar/c',
+ fuA: v
+ };
+ });
+
+ define('foo/b',['require', './bar/c'], function(require){
+ return {
+ name: 'foo/b',
+ barC: require('./bar/c')
+ };
+ });
+
+ define('main', ['./foo/b'], function(v){
+ return {
+ name: 'main',
+ fooB: v
+ };
+ });
+
+ var myMain = null;
+
+ require(['main'], function(main){
+ myMain = main;
+ doh.is('main', myMain.name);
+ doh.is('foo/b', myMain.fooB.name);
+ doh.is('foo/bar/c', myMain.fooB.barC.name);
+ doh.is('fu/a', myMain.fooB.barC.fuA.name);
+ done();
+ }, function (err) {
+ //This should never be reached, but it should not cause
+ //a problem either.
+ throw err;
+ });
+
+ //Make sure require is async
+ doh.is(null, myMain);
+
+ </script>
+</head>
+<body>
+ <h1>almond: errback Test</h1>
+
+ <p>Test the use of errbacks in a require call. While almond does not
+ support calling an errback (all modules should be built in and usable),
+ almond should not throw any errors if they are used.</p>
+
+ <p>Check console for messages</p>
+</body>
+</html>

0 comments on commit f7162da

Please sign in to comment.