Skip to content

Instantly share code, notes, and snippets.

@boazsender
Created December 5, 2010 16:44
Show Gist options
  • Save boazsender/729227 to your computer and use it in GitHub Desktop.
Save boazsender/729227 to your computer and use it in GitHub Desktop.
Enhanced Singleton pattern borrowed by @rwaldron from jQuery for Popcornjs
/*
* Outline code for the popcornjs enhanced singleton pattern
*/
// Immediately invoked function expression is used to
// create a pseudo-private scope for our api definition
// a reference to the `window` object is passed into the
// closure and referenced as `global`. This is both
// beneficial for compressors and provides a fast
// reference to the window context
(function(global) {
// Cache refs to speed up calls to native utils
var
forEach = Array.prototype.forEach,
hasOwn = Object.prototype.hasOwnProperty,
slice = Array.prototype.slice,
// ID string matching
rIdExp = /^(#([\w\-\_\.]+))$/,
// Declare a pseudo-private constructor
// This constructor returns the instance object.
Popcorn = function( entity ) {
// Return new instance of Popcorn.prototype.instance constructor
return new Popcorn.p.init( entity );
};
// Declare a shortcut (Popcorn.p) to and a definition of
// the new prototype for our Popcorn constructor
Popcorn.p = Popcorn.prototype = {
init: function( entity ) {
var elem, matches;
matches = rIdExp.exec( entity );
if ( matches.length && matches[2] ) {
elem = document.getElementById(matches[2]);
}
this.video = elem ? elem : null;
this.data = {
events: {},
tracks: []
};
return this;
}
};
// This trick allows our api methods to be chained to
// instance references.
Popcorn.p.init.prototype = Popcorn.p;
// Plugins are registered
Popcorn.registry = [];
// An interface for extending Popcorn
// with plugin functionality
Popcorn.plugin = function( name, definition ) {
// Handle putting new methods on the Popcorn object
};
global.Popcorn = Popcorn;
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment