Forums

Posting in these forums is disabled. These forums will be available for archive purposes. Please join the new forums at the links below:

  • yui-support - replaces the `YUI 3.x` and `YUI 3 Gallery` forums.
    We have created the following discussion categories within this group to aid discoverability for these most-used topics:
    • Charts for YUI Charts support.
    • DataTable for YUI DataTable support.
    • Gallery for YUI Gallery support, including support for published Gallery components as well as the Gallery process in general.
    • Tools for support of YUI’s suite of developer tools such as selleck, shifter, grover, yogi, etc.
    • Everything Else for questions that don’t fit one of the categories above, we’ve got you covered here.
  • yui-deprecated - replaces the `YUI 2.x` forum and the forums of other deprecated products (`YUI Doc`, `Builder`, `YUI PHP Loader`, etc.).
  [ 5 posts ]
New Topic | Post Reply | Print view
Previous topic | Next topic

Matt Snider

YUI Contributor

  • Username: matt.snider
  • Joined: Fri Jun 19, 2009 11:54 am
  • Posts: 39
  • Location: mountain view,ca
  • GitHub: mattsnider
  • Gists: mattsnider
  • YUI Developer
  • Offline
  • Profile
Tags:
  • delegator
  • events
  • singleton

What is the best way to handle a global singleton in YUI 3?

Post Posted: Thu Jan 20, 2011 10:56 am
I wrote an event delegator for my current application that uses the 'add' function to make it available through the 'use' function elsewhere in my application. The delegator creates a single click event on the document and uses a register function to assign a callback function to a class pattern. However, each YUI 3 instance that uses my delegator class causes another 'click' event to be attached and maintains a separate sandboxed array of patterns and callback functions. To fix this I created a singleton function that attaches the object to window.YUI.

However, I was wondering if there was a built in way in YUI for handling singleton objects that have data that should not be sandboxed between use statements?

Nate Cavanaugh

YUI Contributor

  • Offline
  • Profile

Re: What is the best way to handle a global singleton in YUI

Post Posted: Wed Jan 26, 2011 8:39 am
I've always just stuffed them onto the YUI object itself rather than onto an instance.

Then you could do something like:
YUI.namespace('MATT_SNIDER');
if (!YUI.MATT_SNIDER.myDelegator){
var myDelegator = function(){...};
YUI.MATT_SNIDER.myDelegator = myDelegator;
myDelegator();
}

I *think* that's what you were asking, but I could have missed what you meant :)

Ryan Grove

YUI Developer

  • Username: rgrove
  • Joined: Tue Dec 09, 2008 9:55 pm
  • Posts: 275
  • Location: Portland, OR
  • Twitter: yaypie
  • GitHub: rgrove
  • Gists: rgrove
  • IRC: rgrove
  • YUI Developer
  • Offline
  • Profile
Tags:
  • event
  • examples
  • history
  • jsonp
  • global

Re: What is the best way to handle a global singleton in YUI

Post Posted: Wed Jan 26, 2011 9:39 am
YUI.Env is actually the best place to store data that you need to share globally, and ends up being cleaner than hanging everything off the YUI object itself.. So:

Code:
var global = YUI.namespace('Env.MyModule');
// ... now hang whatever you want off of global ...

There are lots of examples of this in the core lib, but a couple that come to mind are JSONP, which hangs global callback functions off of YUI.Env.JSONP; and History, which stores global history state and event subscriber info in YUI.Env.History and Y.Env.HistoryHash.

Matt Snider

YUI Contributor

  • Username: matt.snider
  • Joined: Fri Jun 19, 2009 11:54 am
  • Posts: 39
  • Location: mountain view,ca
  • GitHub: mattsnider
  • Gists: mattsnider
  • YUI Developer
  • Offline
  • Profile

Re: What is the best way to handle a global singleton in YUI

Post Posted: Wed Jan 26, 2011 10:45 am
Nate and Ryan thanks for the answers.

-matt

Ryan Grove

YUI Developer

  • Username: rgrove
  • Joined: Tue Dec 09, 2008 9:55 pm
  • Posts: 275
  • Location: Portland, OR
  • Twitter: yaypie
  • GitHub: rgrove
  • Gists: rgrove
  • IRC: rgrove
  • YUI Developer
  • Offline
  • Profile

Re: What is the best way to handle a global singleton in YUI

Post Posted: Wed Jan 26, 2011 12:42 pm
FYI, Luke reminded me that Y.Global (which is provided by the event-custom module) is also shared across instances, and may end up being a cleaner way of sharing globals, depending on your needs.

The main distinction is that YUI.Env exists before any YUI instances are created, whereas Y.Global only exists after there's at least one YUI instance on the page, and is used mainly for events that are broadcast globally.
  [ 5 posts ]
New Topic | Post Reply | Print view
Previous topic | Next topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum