singleton use?

29 views
Skip to first unread message

Patrick Mulder

unread,
Apr 3, 2014, 4:08:52 PM4/3/14
to thor...@googlegroups.com
Hi,

in one of the feedback on my book, I got a question why it would make sense to use a singleton:

  var instance;
  Layout.getInstance = function() {

    if (!instance) {
      instance = new Layout();
    }
    return instance;

  }
  module.exports = Layout;


instead of:

  var instance;
  Layout.getInstance = function() {

    if (instance) {
      instance.remove();
      instance = new Layout();
    }
    return instance;

  }

As I actually saw the singleton idea in an example on Thorax, I wonder if someone here has a pointer, to what is better.
In Chaplin, there are controllers for setting up layouts, if I understood correctly.

Any ideas, suggestions?

Thanks!

Patrick

Erik Trom

unread,
Apr 7, 2014, 10:23:18 PM4/7/14
to thor...@googlegroups.com
Well the second example shown is not actually a singleton because it's re-creating a new layout view every time... 

From what I understand views should always be torn down and re-created anyway, while something like a controller would ideally back a view and maintain application state(browser app state)... That said, in Thorax, the view and the controller are not really separate, the context property contains the state of the view, therefore state should just be maintained on the server... 

Ping me if you want to talk about this topic in more depth... cheers

- Erik

Patrick Mulder

unread,
Apr 8, 2014, 5:33:04 AM4/8/14
to thor...@googlegroups.com
That's helpful, indeed the Singleton should actually be controller then. A pattern that I see in blogs/StackOverflow is that of a "ViewManager". In Thorax, that is the layout, but it is used only "once" (with the closure idea, if I understand).

I am looking for some pointers of re-creating views (and removing them before), e.g. something like http://stackoverflow.com/questions/12181512/backbonejs-remove-old-view-on-back

Others, store a this.currentView property, and re-create and cleanup that one...

Hmm... maybe I stay with the "singleton" approach.

Patrick Mulder

unread,
Apr 8, 2014, 8:02:44 AM4/8/14
to thor...@googlegroups.com
looks like this post shows how to do the thing with close ( remove + unbind):



--
You received this message because you are subscribed to the Google Groups "Thoraxjs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to thoraxjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages