Skip to content

Instantly share code, notes, and snippets.

@obenjiro
Last active November 15, 2015 22:12
Show Gist options
  • Select an option

  • Save obenjiro/6d260afdc36c5d35134b to your computer and use it in GitHub Desktop.

Select an option

Save obenjiro/6d260afdc36c5d35134b to your computer and use it in GitHub Desktop.
ExtJs simplest possible DI ( Dependency Injection )
Ext.application({
name : 'Fiddle',
launch : function() {
var a = DependencyInjector.provide('A');
Ext.Msg.alert('Fiddle', a.getString());
}
});
Ext.define('DependencyContainer', {
singleton: true,
deps: {
A: {
prop: 'C'
},
C: {
oldValObj: 'B'
}
}
})
Ext.define('DependencyInjector', {
singleton: true,
provide: function(className) {
var clazz = DependencyContainer.deps[className];
var depsConfig = {};
if (clazz) {
var keys = Object.keys(clazz);
keys.forEach(function(key) {
depsConfig[key] = this.provide(clazz[key])
}, this);
}
var obj = Ext.create(className)
return Ext.apply(obj, depsConfig);
}
});
Ext.define('A', {
getString: function() {
return this.prop.getValue();
}
})
Ext.define('B', {
getValue: function() {
return 'Welcome to Sencha Fiddle!'
}
})
Ext.define('C', {
getValue: function() {
return this.oldValObj.getValue() + ' TTT!!!';
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment