Skip to content

Commit

Permalink
See GeppettoJS#51, resolve dependencies before calling initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
creynders authored and mmikeyy committed Sep 25, 2014
1 parent 64da9e3 commit dd827e3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
27 changes: 16 additions & 11 deletions backbone.geppetto.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
Resolver.prototype = {
_createAndSetupInstance: function(Clazz, wiring) {
var instance = new Clazz();
this.resolve(instance, wiring);
if (!instance.initialize) {
this.resolve(instance, wiring);
}
return instance;
},

Expand Down Expand Up @@ -69,15 +71,18 @@
},

_wrapConstructor: function(OriginalConstructor, wiring) {
if (OriginalConstructor.prototype.initialize) {
var context = this._context;

var context = this._context;

return OriginalConstructor.extend({
initialize: function() {
context.resolver.resolve(this, wiring);
OriginalConstructor.prototype.initialize.apply(this, arguments);
}
});
return OriginalConstructor.extend({
initialize: function() {
context.resolver.resolve(this, wiring);
OriginalConstructor.prototype.initialize.apply(this, arguments);
}
});
} else {
return OriginalConstructor;
}
},


Expand All @@ -100,7 +105,7 @@

wireClass: function(key, clazz, wiring) {
this._mappings[key] = {
clazz: clazz,
clazz: this._wrapConstructor(clazz, wiring),
object: null,
type: TYPES.OTHER,
wiring: wiring
Expand All @@ -120,7 +125,7 @@
wireSingleton: function(key, clazz, wiring) {

this._mappings[key] = {
clazz: clazz,
clazz: this._wrapConstructor(clazz, wiring),
object: null,
type: TYPES.SINGLETON,
wiring: wiring
Expand Down
2 changes: 1 addition & 1 deletion dist/backbone.geppetto.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion specs/src/resolver-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,12 @@ define([
var clazz = function(){
clazzInstantiated++;
};
var resolvedDependency;
var singleton = Backbone.Model.extend({
wiring : ['clazz']
wiring : ['clazz'],
initialize : function(){
resolvedDependency = this.clazz;
}
});
beforeEach(function(){
clazzInstantiated=0;
Expand All @@ -380,6 +384,10 @@ define([
var actual = resolver.getObject('singleton');
expect(clazzInstantiated ).to.equal(1);
});
it("should resolve dependencies before initialization", function(){
var actual = resolver.getObject('singleton');
expect(resolvedDependency ).to.be.instanceOf(clazz);
});
});
});

Expand Down

0 comments on commit dd827e3

Please sign in to comment.