-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOM.js
63 lines (52 loc) · 1.39 KB
/
DOM.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* DOM.js - Cache for DOM Objects
* Dual licensed under MIT and GPL.
* Date: 25/12/2012
* @author Joaquin Marti <joaquinmarti.com>
* @version 1.0
* @memberOf jQuery
* @example
* DOM.get('#nav'); // Get an element querying the DOM or from cache
* DOM.clear('#nav); // Clear the object from Cache
* DOM.refresh(); // Delete all Cache references
* DOM.live('#nav'); // Get an element from live DOM, without cache
*
* https://github.com/joaquinmarti/DOM
*/
;var DOM = (function($, undefined) {
var _cache = [];
// Get a cached jquery object
var _get = function(selector) {
// Check if the selector is valid
if (!selector || typeof selector !== 'string') {
return false;
}
// If the object doesn't exist in _cache, query the dom to get it
if (!_cache[selector]) {
_cache[selector] = $(selector); // _Cache the object from selector
}
return _cache[selector];
};
// Remove an object from _cache
var _clear = function(selector) {
if (_cache[selector]) {
delete _cache[selector]; // _Cache the object from selector
return true;
}
return false;
};
// Refresh all _cache vars
var _refresh = function() {
_cache = [];
};
// Get the object without cache
var _live = function(selector) {
return $(selector);
};
return {
get: _get,
clear: _clear,
refresh: _refresh,
live: _live
};
})(jQuery);