recipeswithbackbone / recipeswithbackbone.github.com

Marketing site for the best damn backbone.js book evar
23 stars 1 forks source link

Singleton View should be Anonymous View #21

Closed ngauthier closed 12 years ago

ngauthier commented 12 years ago

A Singleton pattern is for an object that should only be instantiated once but is references in many places. It is a cleaner way of doing a global variable.

However, in our case, it's more of an anonymous view. In many cases a reference to the view is not needed and rarely do other pieces of code need to call methods on the view. It should be instantiated with a model or collection and an element and then not referenced again (except potentially in the router, but this can be done by instantiating the view to a local variable and passing it to the router).

ngauthier commented 12 years ago

additional thoughts:

can't do it as a local because then we'd have to define the view in the bootstrap file which would be gross. Alternatively we could use a setter on the router and set it when it's instantiated. E.g.:

MyCalendar.Router.setCreateEventView(new (Backbone.View.extend({
  // ...
}))({el: $('#create-event')}));
ngauthier commented 12 years ago

Actually I settled on Instantiated View.