Skip to content

Instantly share code, notes, and snippets.

@mikemclin
Created January 7, 2014 16:37
Show Gist options
  • Save mikemclin/8302090 to your computer and use it in GitHub Desktop.
Save mikemclin/8302090 to your computer and use it in GitHub Desktop.
Module method for caching jQuery selectors
(function(window, document, $, undefined){
var self = $.Module = {};
self.init = function() {
this.cache();
};
self.cache = function() {
var _storage = {};
self.cache = {
$: function(selector, clear) {
clear = ( typeof clear !== 'undefined' ) ? clear : false;
if ( typeof _storage[selector] !== 'undefined' && ! clear ) {
return _storage[selector];
} else {
return _storage[selector] = $(selector);
}
},
get: function() {
return _storage;
},
flush: function() {
_storage = {};
}
};
};
self.init();
}(this, this.document, this.jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment