Intravenous+Backbone

65 views
Skip to first unread message

erick p

unread,
Jul 16, 2012, 5:57:14 PM7/16/12
to intrav...@googlegroups.com
Thanks for the great work on this! Was looking for something like this for Javascript as I was a huge fan of DI done in Robotlegs for AS3!

Quick question:
I've got a backbone project that I'd like to use DI with. I've used your framework and I can sort of get it to work the way I want but alas, I feel
that the way I'm using your injection is more as a global repository rather than as straight DI within classes.

Basically my issue is how can I pass the required dependencies to my Backbone classes?

At the moment I'm creating the container class, assigning dependencies..but...
then I add the injector to my custom command & view classes via the prototype:
_.extend(Drafted.Commands.Base.prototype.injector, container);//all command classes inherit injector
_.extend(Drafted.Views.QuestionSliderView.prototype.injector, container);

Within my classes I get the dependencies i need via something like this:
var currItem = this.injector.get("currentQuestionItem");

I feel i'm not going about it the right way :(
Following your example I'd like to be able to access the dependencies as if they were already declared in the class.
Any advice?? :)

Once again thx for a great framework!
cheers
erick

Roy Jacobs

unread,
Jul 17, 2012, 4:46:39 AM7/17/12
to intrav...@googlegroups.com
Hi Erick,

Thanks for the feedback! Intravenous is still at quite an early stage so there may certainly be areas where some additional work is needed to do nicer integrations which existing libraries and so on.

Could you perhaps put up a small test-case somewhere on jsfiddle or github? That way it would be easier to play around with things. Also, I'm not familiar with Backbone (am a Knockout guy myself :)) so I definitely need to see some more code in order to help you.

What you are currently doing is using the container as a service locator, which some people (though not all!) consider an anti-pattern. It's by no means bad, but ideally your main code should not be aware of the container. But again, I need to see a bit more code in order to help you :)

With regards,
Roy

erick p

unread,
Jul 18, 2012, 10:15:23 AM7/18/12
to intrav...@googlegroups.com
Hi Roy,
Thanks for the reply!

Even though intravenous is pretty new, I really like it's implementation and what it has to offer. Hence the need and desire to get it working properly in my app ;)

I agree with you completely that the way I'm using the injector at the moment is as a "service locator" which is something I don't want. 
I'd rather set up the dependencies I need with the injector and just have them available when I need them without calling the injector directly.

I've set up a gist here:
https://gist.github.com/3136338

It briefly shows the initialization of the injector and the command class that uses the injector.

Thanks again for all your help and effort with this!

cheers
erick

Roy Jacobs

unread,
Jul 18, 2012, 10:27:30 AM7/18/12
to intrav...@googlegroups.com
From (very briefly!) googling, I believe you should be able to add your own constructor to your models using Backbone, so that would already help.
However, Backbone is in control of instantiating your objects, and it should use the container for instantiation, so I'm not sure if that behavior can be swapped out or not?

If you can't change that, then using a service locator pattern is fine.

Roy Jacobs

unread,
Jul 18, 2012, 10:36:29 AM7/18/12
to intrav...@googlegroups.com
I looked at the Backbone code, and it seems that all you would have to do is replace the "clone" methods in Backbone.Collection and Backbone.Model since they seem responsible for object instantiation.

erick p

unread,
Jul 18, 2012, 11:39:40 AM7/18/12
to intrav...@googlegroups.com
nice! i'll give it a try and let you know how it goes Roy!
thx again :)
erick

Roy Jacobs

unread,
Jul 18, 2012, 11:41:29 AM7/18/12
to intrav...@googlegroups.com
You're welcome! Please let me know how it goes, maybe we can use this knowledge to create a Backbone example.

erick p

unread,
Jul 18, 2012, 4:35:15 PM7/18/12
to intrav...@googlegroups.com
Well after looking through the BackboneJS source I've opted for the easy way out ;)
I'm pretty sure there's a more elegant way to implement this but at the moment my mind is bent!
So here's what I did:

           var descModel = function(_currentQuesVal, _currentSlideIndex) {
               return new Drafted.Models.DescriptionModel({currentQuesVal:_currentQuesVal, currentSlideIndex:_currentSlideIndex});
           }

           descModel.$inject = ["currentQuesVal", "currentSlideIndex"];
           container.register("descModel", descModel);

           var ddd = container.get("descModel");
           console.log(ddd) 

I just used your example and wrapped the instantiation of the model within a new function.
I'd love to see a cleaner way but for now this'll do!
thx again for the inspiration ;)
cheers
erick

Roy Jacobs

unread,
Jul 19, 2012, 7:08:10 AM7/19/12
to intrav...@googlegroups.com
That's quite verbose, but if this works for you it should be fine. Maybe it's still worth investigating the replacement of the 'clone' methods, though. Hopefully I'll have some time do do that sometime.

Good to hear you have it working, at least :)

erick p

unread,
Jul 19, 2012, 11:09:09 AM7/19/12
to intrav...@googlegroups.com
You're right! it was a bit much for me as well so I spent some time thinking about it ;)
Came up with this test class:

Drafted.Commands.TestCommand = function(_playerStats, _currentScore) {

    return Backbone.Model.extend({
            dummy: '',
            playerStats: '',

            initialize: function(_playerStats, _currentScore){
                this.dummy = _currentScore;
                this.playerStats = _playerStats;
                console.log(this.dummy);
                console.log(this.playerStats);
            }

    });
}

My issue was trying to figure out how the parameters get passed into an actual class.
Figured to wrap the class in a dummy function that passes it down to a returned Model class.
Again for now it'll do :)
Looking forward to your implementation ;)
Once again thx for the great DI :)
cheers
erick
Reply all
Reply to author
Forward
0 new messages