Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

new builds

  • Loading branch information...
commit e4890508f4a7dbf14956be14399f169193a2efd2 1 parent 785db35
Christoph Burgdorf authored
Showing with 51 additions and 8 deletions.
  1. +50 −7 build/swiftcore.js
  2. +1 −1  build/swiftcore.min.js
57 build/swiftcore.js
View
@@ -92,17 +92,29 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
return this;
};
+ Registration.prototype.withOptions = function(options){
+ this.options = options;
+ return this;
+ };
var hasDependencies = function(registration) {
return !(registration.type.dependencies === undefined || registration.type.dependencies.length === 0);
};
- var createInstanceOrReuseExistingOne = function(registration, dependencies){
+ var createInstanceOrReuseExistingOne = function(registration, options){
if (registration.singleton && registration.isInitialized){
return registration.instance;
}
- var instance = swiftcore.defaultInstanceProvider(registration, dependencies);
+ //Todo think about if that's really the right way to handle options. It could be problematic that the
+ //instance provider has no *convenient* way to see which parts of the object were dependencies
+ //and what were registered options.
+
+ //think about swiftcore.defaultInstanceProvider(registration, cleanOptions, extendedOptions);
+
+ deepExtend(options, registration.options);
+
+ var instance = swiftcore.defaultInstanceProvider(registration, options);
if (registration.singleton){
registration.instance = instance;
}
@@ -131,7 +143,41 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
var trimAndLowerCase = function(str){
return str.trim().toLowerCase();
- }
+ };
+
+ //this code is stolen from here: http://noteslog.com/post/how-to-force-jqueryextend-deep-recursion/
+ var deepExtend = function() {
+ var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options;
+
+ if (target.constructor == Boolean) {
+ deep = target;
+ target = arguments[1] || {};
+ i = 2;
+ }
+
+ if (typeof target != "object" && typeof target != "function")
+ target = {};
+
+ if (length == 1) {
+ target = this;
+ i = 0;
+ }
+
+ for (; i < length; i++)
+ if ((options = arguments[i]) != null)
+ for ( var name in options ) {
+ if (target === options[name])
+ continue;
+
+ if (deep && options[name] && typeof options[name] == "object" && target[name] && !options[name].nodeType)
+ target[name] = deepExtend(true, target[name], options[name]);
+
+ else if (options[name] != undefined )
+ target[name] = options[name];
+ }
+
+ return target;
+ };
swiftcore.register = function(name, type, singleton){
return swiftcore.addRegistration({
@@ -143,10 +189,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
swiftcore.addRegistration = function(registration){
var newRegistration = new Registration();
- newRegistration.name = registration.name;
- newRegistration.type = registration.type;
- newRegistration.singleton = !!registration.singleton;
-
+ deepExtend(newRegistration, registration);
store[trimAndLowerCase(newRegistration.name)] = newRegistration;
return newRegistration
};
2  build/swiftcore.min.js
View
@@ -8,4 +8,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-*/(function(){window.swiftcore={}})()(function(){if(!window.swiftcore)throw"make sure swiftcore.js was loaded before swiftcore.dependencyFormatters.js";var a=window.swiftcore;a.dependencyFormatters={},a.dependencyFormatters.asIs=function(a){return a},a.dependencyFormatters.camelCase=function(a){return a.replace(/(?:^\w|[A-Z]|\b\w)/g,function(a,b){return b==0?a.toLowerCase():a.toUpperCase()}).replace(/\s+/g,"")}})()(function(){if(!window.swiftcore)throw"make sure swiftcore.js was loaded before swiftcore.instanceProvider.js";var a=window.swiftcore;a.instanceProvider={},a.instanceProvider.constructorBased=function(a,b){var c=new a.type(b);return a.isInitialized=!0,c},a.instanceProvider.initializeBased=function(a,b){return a.instance||(a.instance=new a.type),a.instance.initialize(b),a.isInitialized=!0,a.instance}})()(function(){function c(){}var a=window.swiftcore,b={};a.defaultInstanceProvider=a.instanceProvider.constructorBased,a.defaultDependencyFormatter=a.dependencyFormatters.asIs,c.prototype.register=function(a){return this.name=a,this},c.prototype.withType=function(a){return this.type=a,this},c.prototype.withInstance=function(a){return this.instance=a,this.isInitialized=a!==undefined&&a!==null,this},c.prototype.asSingleton=function(){return this.singleton=!0,this};var d=function(a){return a.type.dependencies!==undefined&&a.type.dependencies.length!==0},e=function(b,c){if(b.singleton&&b.isInitialized)return b.instance;var d=a.defaultInstanceProvider(b,c);return b.singleton&&(b.instance=d),d},f=function(b){var c={};for(var g in b){var h=b[g],i=a.getRegistration(h),j=a.defaultDependencyFormatter(h);if(!d(i))c[j]=e(i);else{var k=f(i.type.dependencies);c[j]=e(i,k)}}return c},g=function(a){return a.trim().toLowerCase()};a.register=function(b,c,d){return a.addRegistration({type:c,singleton:!!d,name:b})},a.addRegistration=function(a){var d=new c;return d.name=a.name,d.type=a.type,d.singleton=!!a.singleton,b[g(d.name)]=d,d},a.getRegistration=function(a){return b[g(a)]},a.resolve=function(b){var c=a.getRegistration(b);if(c===undefined)throw"Failed to resolve: "+b+". Registration unknown";if(c.type===undefined)throw"Failed to resolve: "+b+". Registration has no constructor function";var d=c.type.dependencies||[],d=f(d);return e(c,d)}})();
+*/(function(){window.swiftcore={}})()(function(){if(!window.swiftcore)throw"make sure swiftcore.js was loaded before swiftcore.dependencyFormatters.js";var a=window.swiftcore;a.dependencyFormatters={},a.dependencyFormatters.asIs=function(a){return a},a.dependencyFormatters.camelCase=function(a){return a.replace(/(?:^\w|[A-Z]|\b\w)/g,function(a,b){return b==0?a.toLowerCase():a.toUpperCase()}).replace(/\s+/g,"")}})()(function(){if(!window.swiftcore)throw"make sure swiftcore.js was loaded before swiftcore.instanceProvider.js";var a=window.swiftcore;a.instanceProvider={},a.instanceProvider.constructorBased=function(a,b){var c=new a.type(b);return a.isInitialized=!0,c},a.instanceProvider.initializeBased=function(a,b){return a.instance||(a.instance=new a.type),a.instance.initialize(b),a.isInitialized=!0,a.instance}})()(function(){function c(){}var a=window.swiftcore,b={};a.defaultInstanceProvider=a.instanceProvider.constructorBased,a.defaultDependencyFormatter=a.dependencyFormatters.asIs,c.prototype.register=function(a){return this.name=a,this},c.prototype.withType=function(a){return this.type=a,this},c.prototype.withInstance=function(a){return this.instance=a,this.isInitialized=a!==undefined&&a!==null,this},c.prototype.asSingleton=function(){return this.singleton=!0,this},c.prototype.withOptions=function(a){return this.options=a,this};var d=function(a){return a.type.dependencies!==undefined&&a.type.dependencies.length!==0},e=function(b,c){if(b.singleton&&b.isInitialized)return b.instance;h(c,b.options);var d=a.defaultInstanceProvider(b,c);return b.singleton&&(b.instance=d),d},f=function(b){var c={};for(var g in b){var h=b[g],i=a.getRegistration(h),j=a.defaultDependencyFormatter(h);if(!d(i))c[j]=e(i);else{var k=f(i.type.dependencies);c[j]=e(i,k)}}return c},g=function(a){return a.trim().toLowerCase()},h=function(){var a=arguments[0]||{},b=1,c=arguments.length,d=!1,e;a.constructor==Boolean&&(d=a,a=arguments[1]||{},b=2),typeof a!="object"&&typeof a!="function"&&(a={}),c==1&&(a=this,b=0);for(;b<c;b++)if((e=arguments[b])!=null)for(var f in e){if(a===e[f])continue;d&&e[f]&&typeof e[f]=="object"&&a[f]&&!e[f].nodeType?a[f]=h(!0,a[f],e[f]):e[f]!=undefined&&(a[f]=e[f])}return a};a.register=function(b,c,d){return a.addRegistration({type:c,singleton:!!d,name:b})},a.addRegistration=function(a){var d=new c;return h(d,a),b[g(d.name)]=d,d},a.getRegistration=function(a){return b[g(a)]},a.resolve=function(b){var c=a.getRegistration(b);if(c===undefined)throw"Failed to resolve: "+b+". Registration unknown";if(c.type===undefined)throw"Failed to resolve: "+b+". Registration has no constructor function";var d=c.type.dependencies||[],d=f(d);return e(c,d)}})();
Please sign in to comment.
Something went wrong with that request. Please try again.