Comparing version 1.2.2 to 1.3.0
# Change History | ||
## 1.3.0 (2014-02-22) | ||
* Can now use `registerInjects()` to have certain properties set immediately | ||
after creation via any factory method on a per-class basis. | ||
## 1.2.2 (2014-02-03) | ||
@@ -4,0 +9,0 @@ |
@@ -16,2 +16,6 @@ module.exports = Container; | ||
this._types = {}; | ||
// Injected post-instance | ||
this._injects = []; | ||
this.register('container', this); | ||
@@ -112,2 +116,14 @@ } | ||
/** | ||
* For a given type that comes out of a factory, assign instance properties to | ||
* it everytime. | ||
* @param {Function} T The class. | ||
* @param {Object.<string,*>} injectMap | ||
*/ | ||
Container.prototype.registerInjects = function(T, injectMap) | ||
{ | ||
var map = this._getInjectMap(T); | ||
_.extend(map, injectMap); | ||
}; | ||
/** | ||
* Request the object provided by the container registed under construct T. | ||
@@ -139,3 +155,3 @@ * @param {Function} T Constructor function we want to find the factory for. | ||
var deps = this._resolveDeps(getArgs(T)); | ||
return callNew(T, deps); | ||
return this._doInjects(T, callNew(T, deps)); | ||
}; | ||
@@ -176,3 +192,3 @@ | ||
if (entry.T) { | ||
instance = callNew(entry.T, deps); | ||
instance = this._doInjects(entry.T, callNew(entry.T, deps)); | ||
} else { | ||
@@ -202,3 +218,29 @@ instance = entry.closure.call(null, deps); | ||
Container.prototype._doInjects = function(T, instance) | ||
{ | ||
var map = this._getInjectMap(T); | ||
_.extend(instance, map); | ||
return instance; | ||
}; | ||
/** | ||
* Get any inject maps for instances based on class T | ||
* @param {Function} T | ||
* @return {Object.<String,*>} | ||
*/ | ||
Container.prototype._getInjectMap = function(T) | ||
{ | ||
for (var n = 0; n < this._injects.length; n++) { | ||
var entry = this._injects[n]; | ||
if (entry.T === T) | ||
return entry.map; | ||
} | ||
// New one | ||
var map = {}; | ||
this._injects.push({T: T, map: map }); | ||
return map; | ||
}; | ||
/** | ||
* Resolve a list of string dependencies into real dependencies | ||
@@ -205,0 +247,0 @@ * @param {Array.<String>} deps |
{ | ||
"name": "sack", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "An Inversion-of-Control container for all your dependency injection needs.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -151,2 +151,9 @@ # Sack | ||
You can also register certain properties to be set every time a depedency is | ||
created via any of the factory methods for a specific class. | ||
```javascript | ||
container.registerInjects(MyService, { securityToken: '123-123' }); | ||
``` | ||
### Resolving Objects | ||
@@ -153,0 +160,0 @@ |
27358
12
400
235