Skip to content
Browse files

Merge branch 'deepCopy'

  • Loading branch information...
2 parents f67e4d5 + ead7bb0 commit 785db350b5f78b11cf7fb12a93a8a451ff1a0cf1 @cburgdorf committed Apr 1, 2012
Showing with 94 additions and 7 deletions.
  1. +50 −7 src/swiftcore.js
  2. +44 −0 tests/swiftcore.tests.js
View
57 src/swiftcore.js
@@ -28,17 +28,29 @@
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;
}
@@ -67,7 +79,41 @@
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({
@@ -79,10 +125,7 @@
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
};
View
44 tests/swiftcore.tests.js
@@ -23,6 +23,20 @@ test('can resolve types', function () {
ok(instance.test === "foo");
});
+test('keeps user defined registration properties', function () {
+
+ function SomeType() {
+ this.test = "foo";
+ }
+
+ swiftcore.addRegistration({name: "someType", type: SomeType, userDefinedProperty: true });
+ var registration = swiftcore.getRegistration("someType");
+
+ equal(registration.name, "someType");
+ equal(registration.type, SomeType)
+ equal(registration.userDefinedProperty, true);
+});
+
test('resolves types insensitive to case or whitespace', function () {
function SomeType() {
@@ -102,6 +116,36 @@ test('can resolve types depending on other types', function () {
ok(instance.test === "foo");
});
+test('can resolve with additional options', function () {
+
+ function TypeA() {
+ }
+
+ function SomeType(options) {
+ if (options.TypeA === undefined) {
+ throw "missing argument [typeA]"
+ }
+
+ if (options.someOption === undefined){
+ throw "missing argument [someOption]";
+ }
+
+ if (Object.keys(options).length !== 2){
+ throw "unexpected arguments"
+ }
+
+ this.test = "foo";
+ }
+
+ SomeType.dependencies = ["TypeA"];
+
+ swiftcore.register("TypeA", TypeA);
+ swiftcore.register("SomeType", SomeType).withOptions({someOption: true});
+ var instance = swiftcore.resolve("SomeType");
+ ok(instance !== undefined);
+ ok(instance.test === "foo");
+});
+
test('can resolve types depending on other types (3 levels)', function () {
function TypeA() {

0 comments on commit 785db35

Please sign in to comment.
Something went wrong with that request. Please try again.