javascript - Where should I put the Collection instance in Backbone.js? -
is best place in global object or within view instance?
(i'm thinking if place on global such as: window.todos = new todos()
, pollute global object if there lot of collections; however, putting inside view may make things bit confusing)
based on experience, best way place it?
i think depends on application. have done establish namespace application, , put collections there, views:
// namespace application var app = {}; // collection classes within namespace app.todos = backbone.collection.extend({ ... }); // , collection instances in namespace singletons app.todos = new app.todos();
for large application, might go further, establishing sub-namespaces model classes, view classes, router classes, , application instances. there few advantages this, though none of them crucial:
having collections available in
app
namespace allows them referenced multiple views if necessarywhen debugging, have access collections in console
establishing new namespace application allows add number of new global variables without polluting global namespace or worrying bad interactions other libraries
the last point argument namespacing app, doesn't follow collections need @ top level of namespace, or available globally @ all. main question how important able access collection outside particular parent view (or parent model). if you're sure collection one-off, might cleaner keep out of app namespace entirely.
Comments
Post a Comment