Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Created November 20, 2012 19:51
Show Gist options
  • Save JeffreyWay/4120585 to your computer and use it in GitHub Desktop.
Save JeffreyWay/4120585 to your computer and use it in GitHub Desktop.
What do you prefer?
// 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;
@davemo
Copy link

davemo commented Nov 20, 2012

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 :)

@JeffreyWay
Copy link
Author

@dave - Yeah, you're right. Forgot to fix that. Will do now.

@redbaron76
Copy link

Definitely inside my Router:

var myCollection = new SomeCllection();
new SomeView({collection: myCollection});

Gobal pollution must be avoided as musch as possible.

@mencosk
Copy link

mencosk commented Nov 21, 2012

first one

@sergiomedinag
Copy link

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