Delayed dependency injection

Skip to first unread message

Paul Thomson

May 9, 2014, 11:19:36 AM5/9/14
DeftJS is the first time I have taken a serious look at dependency injection, so I think the following question might stem from a slight misunderstanding of what Dependency Injection (normally) is.

Does DeftJS provide any way for injecting the dependencies after an object has been created? Specifically, at a certain point when the object requests the object (i.e. creates it). I have now discovered that this is not typical Dependency Injection behaviour because normally the dependency is resolved before the object is created. However, in some situations the dependency cannot be resolved until the first object has been created (for example, if the dependency is an ExtJS DropTarget, which needs the DOM element in the constructor). Here is some example code:

Ext.define('MyDropTarget', {
: ['dropTarget'],
: function() {
this.dropTarget.create(this.el.dom); // in reality this may need to be inside a 'boxready' listener

From what I have seen I don't think DeftJS provides behaviour like this, is there anything I am missing?

The closest I can get is using a factory function, since that has access to the object that was created, but in some cases the dependency can only be resolved after some period of time. Even then, using this method wouldn't be desirable because it would move this logic into the factory function, but it might be more appropriate in the object itself.

From my research it seems like this isn't pure dependency injection, because some of the dependency remains in the fact that we have to create the dependency. Could something like this be added to DeftJS?

Brian Kotek

May 9, 2014, 11:56:10 AM5/9/14
to deftjs
So first, to answer your question, automatic injections can only be done at object creation. However, you can always use var myDep = Deft.Injector.resolve( 'myDependencyIdentifier' ) to grab an injectable dependency if you need to do it manually.

That said, looking at your code, is dropTarget some sort of utility object, or is it a view element? Because if it's a view element, you would not use IoC for this. 

Deft JS home page:
For rules and tips on asking questions on the Deft JS mailing list:
You received this message because you are subscribed to the Google Groups "Deft JS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
For more options, visit

Brian Kotek

May 9, 2014, 12:18:43 PM5/9/14
to deftjs
Ah, I see, it's a DropTarget class. 

In that case, my first question might be: is this really something you need to inject? Since each DropTarget is tied to one and only one element, I'm not sure what you're gaining by trying to inject this rather than just do new DropTarget( el )?

Brian Kotek

May 9, 2014, 12:23:12 PM5/9/14
to deftjs
One more idea: if you really want this injected (maybe to easily swap it out for testing or something), you could do this using an identifier value mapping in your IoC config (

Something like:

    dropTargetClass: {
        value: Ext.dd.DropTarget

Ext.define('MyDropTarget', {
    inject: ['dropTargetClass'],
    constructor: function() {
Reply all
Reply to author
0 new messages