Skip to content

Commit

Permalink
Enhanced IoC container to support classes defined as singletons using…
Browse files Browse the repository at this point in the history
… the Sencha class system.

Modified Deft.ioc.DependencyProvider to properly resolve and detect error conditions when a class is specified that was defined as a singleton using the Sencha class system.
Added associated Jasmine tests for configuring and injecting classes defined as singletons using the Sencha class system.
Updated version history in README.md.

Fixes #11
  • Loading branch information
John Yanarella committed May 14, 2012
1 parent 3222720 commit 85e7cf3
Show file tree
Hide file tree
Showing 7 changed files with 480 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ Provided the specified `controller` extends `Deft.mvc.ViewController`, the contr

# Version History

* 0.6.5 - Enhanced IoC container to support classes defined as singletons using the Sencha class system.
* 0.6.4 - Hotfix for Sencha Touch Logger issue.
* 0.6.3 - Added memoization feature. Fixed reported Sencha Touch issues.
* 0.6.2 - Added support for View Controller event listener options. Ext JS 4.1rc3 compatibility fixes.
Expand Down
74 changes: 48 additions & 26 deletions build/deft-debug.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
DeftJS 0.6.4
DeftJS 0.6.5
Copyright (c) 2012 [DeftJS Framework Contributors](http://deftjs.org)
Open source under the [MIT License](http://en.wikipedia.org/wiki/MIT_License).
Expand Down Expand Up @@ -80,46 +80,47 @@ Ext.define('Deft.util.Function', {
});

/**
@private
@private
Used by {@link Deft.ioc.Injector}.
*/

Used by {@link Deft.ioc.Injector}.
*/
Ext.define('Deft.ioc.DependencyProvider', {
requires: ['Deft.log.Logger'],
config: {
identifier: null,
/**
Class to be instantiated, by either full name, alias or alternate name, to resolve this dependency.
*/
Class to be instantiated, by either full name, alias or alternate name, to resolve this dependency.
*/

className: null,
/**
Optional arguments to pass to the class' constructor when instantiating a class to resolve this dependency.
*/
Optional arguments to pass to the class' constructor when instantiating a class to resolve this dependency.
*/

parameters: null,
/**
Factory function to be executed to obtain the corresponding object instance or value to resolve this dependency.
NOTE: For lazily instantiated dependencies, this function will be passed the object instance for which the dependency is being resolved.
*/
Factory function to be executed to obtain the corresponding object instance or value to resolve this dependency.
NOTE: For lazily instantiated dependencies, this function will be passed the object instance for which the dependency is being resolved.
*/

fn: null,
/**
Value to use to resolve this dependency.
*/
Value to use to resolve this dependency.
*/

value: null,
/**
Indicates whether this dependency should be resolved as a singleton, or as a transient value for each resolution request.
*/
Indicates whether this dependency should be resolved as a singleton, or as a transient value for each resolution request.
*/

singleton: true,
/**
Indicates whether this dependency should be 'eagerly' instantiated when this provider is defined, rather than 'lazily' instantiated when later requested.
NOTE: Only valid when either a factory function or class is specified as a singleton.
*/
Indicates whether this dependency should be 'eagerly' instantiated when this provider is defined, rather than 'lazily' instantiated when later requested.
NOTE: Only valid when either a factory function or class is specified as a singleton.
*/

eager: false
},
Expand All @@ -141,20 +142,35 @@ Ext.define('Deft.ioc.DependencyProvider', {
}
}
if (!this.getSingleton()) {
if (this.getClassName() != null) {
if (Ext.ClassManager.get(this.getClassName()).singleton) {
Ext.Error.raise({
msg: "Error while configuring rule for '" + (this.getIdentifier()) + "': singleton classes cannot be configured for injection as a prototype. Consider removing 'singleton: true' from the class definition."
});
}
}
if (this.getValue() != null) {
Ext.Error.raise({
msg: "Error while configuring '" + (this.getIdentifier()) + "': a 'value' can only be configured as a singleton."
});
}
} else {
if ((this.getClassName() != null) && (this.getParameters() != null)) {
if (Ext.ClassManager.get(this.getClassName()).singleton) {
Ext.Error.raise({
msg: "Error while configuring rule for '" + (this.getIdentifier()) + "': parameters cannot be applied to singleton classes. Consider removing 'singleton: true' from the class definition."
});
}
}
}
return this;
},
/**
Resolve a target instance's dependency with an object instance or value generated by this dependency provider.
*/
Resolve a target instance's dependency with an object instance or value generated by this dependency provider.
*/

resolve: function(targetInstance) {
var instance, parameters;
var classDefinition, instance, parameters;
Deft.Logger.log("Resolving '" + (this.getIdentifier()) + "'.");
if (this.getValue() != null) {
return this.getValue();
Expand All @@ -164,9 +180,15 @@ Ext.define('Deft.ioc.DependencyProvider', {
Deft.Logger.log("Executing factory function.");
instance = this.getFn().call(null, targetInstance);
} else if (this.getClassName() != null) {
Deft.Logger.log("Creating instance of '" + (this.getClassName()) + "'.");
parameters = this.getParameters() != null ? [this.getClassName()].concat(this.getParameters()) : [this.getClassName()];
instance = Ext.create.apply(this, parameters);
classDefinition = Ext.ClassManager.get(this.getClassName());
if (classDefinition.singleton) {
Deft.Logger.log("Using existing singleton instance of '" + (this.getClassName()) + "'.");
instance = classDefinition;
} else {
Deft.Logger.log("Creating instance of '" + (this.getClassName()) + "'.");
parameters = this.getParameters() != null ? [this.getClassName()].concat(this.getParameters()) : [this.getClassName()];
instance = Ext.create.apply(this, parameters);
}
} else {
Ext.Error.raise({
msg: "Error while configuring rule for '" + (this.getIdentifier()) + "': no 'value', 'fn', or 'className' was specified."
Expand Down
8 changes: 5 additions & 3 deletions build/deft.js

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

Loading

0 comments on commit 85e7cf3

Please sign in to comment.