PDA

View Full Version : How to inject reference to application or event bus with Deft?



el_chief
8 Jun 2012, 11:28 AM
My Deft viewcontroller needs to fire application-wide events.

How do I inject the app or app eventbus into a viewcontroller using deft?

Please do not say use a global reference to the application. That defeats the point of using a dependency injection.

el_chief
9 Jun 2012, 2:46 PM
My Deft viewcontroller needs to fire application-wide events.

How do I inject the app or app eventbus into a viewcontroller using deft?

Please do not say use a global reference to the application. That defeats the point of using a dependency injection.

The answer is don't worry about the application bus, and just make your own:


Ext.define('My.app.EventBus', {
mixins:{
observable: 'Ext.util.Observable'
},
constructor: function(){
this.mixins.observable.constructor.call(this);
}
});

Then configure it with Deft:


Deft.ioc.Injector.configure({
'eventBus':'My.app.EventBus' /* is a singleton, by default*/
});

Then inject it in a controller:


Ext.define('My.controller.contacts.Form', {
extend: 'Deft.mvc.ViewController',
mixins: ['Deft.mixin.Injectable'],
inject: ['eventBus'],


config: {
eventBus: null
},


init: function(config){
this.initConfig(config);


this.getEventBus().on('someEvent', function(){ });

this.callParent(arguments);
}
});