Skip to content

Instantly share code, notes, and snippets.

@HichemBenChaaben
Created September 9, 2013 19:20
Show Gist options
  • Save HichemBenChaaben/6500213 to your computer and use it in GitHub Desktop.
Save HichemBenChaaben/6500213 to your computer and use it in GitHub Desktop.
Caching layer for Jquery
/*
*
** Tiny lib to make jQuery times faster
** Usage
** app = new
** app.$('#selector');
* var app = window._APP = new window.app({
* // Caching global window and document objects inside the app object
* "window": window,
* "document": document,
* // jQuery
* "lib": $,
* // Allow logging
* "logging": false
* });
*/
;(function(window, document, undefined){
"use strict";
window.app = window.app || function( settings ) {
this.window = window;
this.document = document;
this.lib = (settings && settings.lib) || window.jQuery;
this.logging = settings && settings.logging;
this.appName = settings && settings.appName;
};
window.app.prototype = {
_cacheElem: {},
log: function( msg ) {
var logStr = this.appName + ": " + msg;
if ( window.console && window.console.log ) {
window.console.log( logStr );
}
},
// localized selector provided in the app scope
$: function( elem, fromCache) {
if ( fromCache === false ) {
this._cacheElem[elem] = this.lib(elem);
}
if ( this._cacheElem[elem] === undefined ) {
this._cacheElem[elem] = this.lib(elem);
}
return this._cacheElem[elem];
},
// localized selector key, val mapping
$$: function( key, elem ) {
if ( !elem ) {
return this._cacheElem[key];
}
if ( this._cacheElem[key] === undefined ) {
this._cacheElem[key] = this.lib(elem);
}
return this._cacheElem[key];
}
};
})( window, document );
var obj = new window.app({ appName: "testapp" });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment