Posted By

johnloy on 02/17/09


Tagged

textmate


Versions (?)

Shared singleton pattern for sharing utility methods across pages


 / Published in: Other
 

  1. /* A similar pattern to the module pattern is the “shared singleton” — create a singleton object for an application that can be used to share properties or methods between pages or inline frames.
  2.  
  3. Here’s the start of a “my.js” script that every page or frame in an application could share: */
  4.  
  5. if (typeof parent.my != “undefined”) {
  6. var my = parent.my; // shared singleton
  7. } else {
  8.  
  9. var my = {};
  10.  
  11. my.something …
  12.  
  13. }
  14.  
  15. /* The “parent” bit is so that the script can be used with iframes and still
  16. retain a reference to the same “my” instance. If a page is at the top, then parent points to itself.
  17.  
  18. Every page or frame that imports “my.js” can share properties or methods with any other page. */

Report this snippet  

You need to login to post a comment.