Created
November 20, 2012 19:51
-
-
Save JeffreyWay/4120585 to your computer and use it in GitHub Desktop.
What do you prefer?
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
// In Backbone, when passing a collection around, do you prefer to inject the | |
// collection into the view, or store it on a global namespace? | |
// Injection | |
var SomeCollectionView = Backbone.View.extend({ | |
initialize: function() { | |
// this.collection | |
} | |
}); | |
new SomeCollectionView({ collection: App.myCollection }); | |
// Or store and reference collection on namespace. | |
var SomeCollectionView = Backbone.View.extend({ | |
initialize: function() { | |
// App.myCollection | |
} | |
}); | |
new SomeCollectionView; |
@dave - Yeah, you're right. Forgot to fix that. Will do now.
Definitely inside my Router:
var myCollection = new SomeCllection();
new SomeView({collection: myCollection});
Gobal pollution must be avoided as musch as possible.
first one
Dependency injection is the best way to handle that. As pointed before, it makes unit testing easier and makes the app more portable.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This example is doing both "injecting the collection" and "storing it on a global namespace". I would put the responsibility of instantiating the collection up at a higher level (like a router or app object) and avoid the global, but I definitely prefer injecting the dependency; it makes unit-testing so much easier :)