Last active
November 15, 2015 22:12
-
-
Save obenjiro/6d260afdc36c5d35134b to your computer and use it in GitHub Desktop.
ExtJs simplest possible DI ( Dependency Injection )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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