Last active
December 22, 2015 16:38
-
-
Save HichemBenChaaben/6500219 to your computer and use it in GitHub Desktop.
Caching layer for Jquery
This file contains 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
/* | |
* | |
** Tiny lib to make jQuery times faster | |
** Usage :::: | |
* 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 | |
* }); | |
** app.$('#selector').doWhatEver(); | |
*/ | |
;(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