alexanderscott/backbone-login

Singleton Pattern

Closed this issue · 1 comments

Here is a quick suggestion. Instead of using app.session = new SessionModel({ }); you can return new SessionModel() in SessionModel.js and that way you can enforce the singleton pattern. No need to set it in app.session. Any place you'd want to use the SessionModel just require it directly. Given the way require works, you would get the already instanciated SessionModel.

@ix-xerri Appreciate the suggestion, but would rather instantiate this manually and assign to the default app global parent.

It is a cool trick that require.js allows for singletons in this manner, but it doesn't allow one to assign default attributes/options to the session model at instantiation via the model's initialize method (i.e. calling new SessionModel( options ) ). More extensible that way.

It also would require (no pun intended) you to require the SessionModel module in every other module which references it, instead of just requiring the app global.

--Alex