diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2014-09-29 18:09:17 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2014-09-29 18:09:17 +1000 |
commit | a66f4025c81d428d0668465c8c7e54e7dd2f3219 (patch) | |
tree | 43293ed4a1e83552857b8797ce9f9244bb9316ea /scorpion.js | |
parent | 1062e09e0359019509728f132539f4604efe5bf8 (diff) |
Improve errors reporting for distant objects, fix requestor (special __requestor__ dep), error out when jQuery can't be found, handle object registration formHEADmaster
Diffstat (limited to 'scorpion.js')
-rw-r--r-- | scorpion.js | 93 |
1 files changed, 63 insertions, 30 deletions
diff --git a/scorpion.js b/scorpion.js index 6d7051c..af15dda 100644 --- a/scorpion.js +++ b/scorpion.js @@ -4,7 +4,7 @@ var Scorpion = (function() { var Deferred = (function() { var defer = function(fn, value) {setTimeout(function() {fn(value);});}; - var runFn = function(fn, deferred, action) { + var runFn = function(fn, deferred) { return function(value) { try { var result = fn(value); @@ -121,6 +121,13 @@ var Scorpion = (function() { return deferred.promise; }; + Deferred.collapseErrorObject = function(obj) { + var errors = []; + for (var key in obj.errors) + errors = errors.concat(obj.errors[key]); + throw errors; + }; + return Deferred; })(); @@ -202,12 +209,23 @@ var Scorpion = (function() { }; Scorpion.prototype.register = function(name, value) { - for (var i = 0, l = this.plugins.length; i < l; ++i) { - if (this.plugins[i].register(this, name, value)) - return this; + if (typeof(name) == "function" || name instanceof Function) { + value = name; + name = value.name; + } + if (typeof(name) == "string" || name instanceof String) { + for (var i = 0, l = this.plugins.length; i < l; ++i) { + if (this.plugins[i].register(this, name, value)) + return this; + } + throw this.error("No plugin handled registration of: " + name); + return this; + } else { + for (var key in name) { + this.register(key, name[key]); + } + return this; } - throw this.error("No plugin handled registration of: " + name); - return this; }; Scorpion.prototype.invoke = function(spec) { @@ -221,13 +239,13 @@ var Scorpion = (function() { var depPromise = this.waitForAll(depPromises); var result = depPromise.then(function(results) { return fn.apply(injector, results); - }); + }, Deferred.collapseErrorObject); result.destroy = function() { return Scorpion.waitForAll(depPromises.map(function(promise) { return promise.destroy(); })).then(function() { return true; - }); + }, Deferred.collapseErrorObject); }; return result; }; @@ -300,13 +318,19 @@ var Scorpion = (function() { DOMPlugin.prototype.get = function(injector, name, stack) { var deferred = injector.defer(); - var interval = setInterval(function() { - var obj = $(this.aliases[name] || name); - if (obj.length) { - clearInterval(interval); - deferred.resolve(obj); - } - }.bind(this), 100); + if (typeof($) !== "undefined" && $) { + var intervalFn = function() { + var obj = $(this.aliases[name] || name); + if (obj.length) { + clearInterval(interval); + deferred.resolve(obj); + } + }.bind(this); + var interval = setInterval(intervalFn, 100); + intervalFn(); + } else { + deferred.reject(new Error("jQuery was not found " + stack.join(" <- "))); + } return deferred.promise; }; @@ -325,13 +349,17 @@ var Scorpion = (function() { HTTPPlugin.prototype.get = function(injector, name, stack) { var deferred = injector.defer(); - $.ajax(this.aliases[name] || name).then(function(result) { - deferred.resolve(result); - // deferred.resolve.bind(deferred); - }, deferred.reject.bind(deferred)); - deferred.promise.destroy = function() { - return Scorpion.resolved(true); - }; + if (typeof($) !== "undefined" && $) { + $.ajax(this.aliases[name] || name).then(function(result) { + deferred.resolve(result); + // deferred.resolve.bind(deferred); + }, deferred.reject.bind(deferred)); + deferred.promise.destroy = function() { + return Scorpion.resolved(true); + }; + } else { + deferred.reject(new Error("jQuery was not found " + stack.join(" <- "))); + } return deferred.promise; }; @@ -358,21 +386,22 @@ var Scorpion = (function() { var parsed = injector.parseSpec(spec); var constructor = parsed[0]; var dependencies = parsed[1]; + var readRequestor = false; var depPromises = dependencies.map(function(dep) { - return injector.get(dep, [name].concat(stack)); + if (dep == "__requestor__") { + readRequestor = true; + return injector.resolved(stack[1]); + } else { + return injector.get(dep, stack); + } }); var depPromise = injector.waitForAll(depPromises); var onDestroy = null; - var readRequestor = false; var deferred = injector.defer(); var result = deferred.promise; depPromise.then(function(results) { var wrappedScorpion = Object.create(injector); - wrappedScorpion.requestor = function() { - readRequestor = true; - return stack[1]; - }; try { deferred.resolve(constructor.apply(wrappedScorpion, results)); } catch (e) { @@ -383,7 +412,11 @@ var Scorpion = (function() { onDestroy(); } }, function(e) { - deferred.reject(injector.error("Error constructing " + name)); + try { + Deferred.collapseErrorObject(e); + } catch (ex) { + deferred.reject(ex); + } }); result.references = 1; @@ -400,7 +433,7 @@ var Scorpion = (function() { return promise.destroy(); })).then(function() { return true; - }); + }, Deferred.collapseErrorObject); } else { return injector.resolved(true); } |