Created
September 5, 2014 22:02
-
-
Save tsvensen/4efedfe486ecbc4d3345 to your computer and use it in GitHub Desktop.
jQuery Element Caching System
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Requires underscore or lodash for isUndefined() | |
var $cache = {}; | |
/** | |
* Returns the page title element and will set the page title element when it doesn't exist | |
* @param {String} selector | |
* @returns {jQuery} | |
*/ | |
getElement: function getElement(selector) { | |
if (typeof selector !== 'string') { | |
throw new Error('Selector must be a string'); | |
} | |
return (_.isUndefined($cache[selector])) ? $cache[selector] = $(selector) : $cache[selector]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment