forked from madrobby/zepto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
40 lines (30 loc) · 945 Bytes
/
data.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
// Zepto.js
// (c) 2010, 2011 Thomas Fuchs
// Zepto.js may be freely distributed under the MIT license.
// The following code is heavily inspired by jQuery's $.fn.data()
(function($) {
var data = {},
uuid = $.uuid = +new Date(),
exp = $.expando = 'Zepto' + uuid;
function isScalar(value) {
return /boolean|number|string/.test(typeof value);
}
function getData(name) {
var id = this[0][exp];
return ( id && data[id] && data[id][name] ) ?
data[id][name] : this.dataAttr(name);
}
function setData(name, value) {
if (isScalar(value)) return this.dataAttr(name, value);
var id = this[0][exp] = ++uuid;
data[id] = data[id] || {};
data[id][name] = value;
return this;
};
$.fn.dataAttr = $.fn.data;
$.fn.data = function(name, value) {
return value === undefined ?
getData.call(this, name) :
setData.call(this, name, value);
};
})(Zepto);