forked from RubyLouvre/mass-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.js
160 lines (157 loc) · 5.95 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//==================================================
// 数据缓存模块
//==================================================
define("data", ["$lang"], function(){
$.log("已加载data模块",7);
var remitter = /object|function/, rtype = /[^38]/;
function innerData( target, name, data, pvt ) {//IE678不能为文本节点注释节点添加数据
if( $.acceptData(target) ){
var id = $.getUid(target), isEl = target.nodeType === 1;
if(name === "@uuid"){
return id;
}
var one = typeof name === "string",//取得指定值
database = isEl ? $["@data"]: target,
table = database[ "@data_"+id ] || (database[ "@data_"+id ] = {
data:{}
});
var _table = table;
//对于用HTML5 data-*属性保存的数据, 如<input id="test" data-full-name="Planet Earth"/>
//我们可能通过$("#test").data("full-name")或$("#test").data("fullName")访问到
if(isEl && !table.parsedAttrs){
var attrs = target.attributes;
//将HTML5单一的字符串数据转化为mass多元化的数据,并储存起来
for ( var i = 0, attr; attr = attrs[i++];) {
var key = attr.name;
if ( key.length > 5 && !key.indexOf( "data-" ) ) {
$.parseData(target, key.slice(5), _table, attr.value);
}//camelize
}
table.parsedAttrs = true;
}
//私有数据都是直接放到table中,普通数据放到table.data中
if ( !pvt ) {
table = table.data;
}
if ( name && typeof name == "object" ) {
$.mix( table, name );//写入一组方法
}else if(one && data !== void 0){
table[ name ] = data;//写入单个方法
}
if(one){
if(name in table){
return table[name]
}else if(isEl && !pvt){
return $.parseData( target, name, _table );
}
}else{
return table
}
}
}
function innerRemoveData (target, name, pvt){
if( $.acceptData(target) ){
var id = $.getUid(target);
if ( !id ) {
return;
}
var clear = 1, ret = typeof name == "string",
database = target.nodeType === 1 ? $["@data"] : target,
table = database["@data_"+id],
cache = table;
if ( table && ret ) {
if(!pvt){
table = table.data
}
if(table){
ret = table[ name ];
delete table[ name ];
}
loop:
for(var key in cache){
if(key == "data"){
for(var i in cache.data){
clear = 0;
break loop;
}
}else{
clear = 0;
break loop;
}
}
}
if(clear){
try{
delete database["@data_"+id];
}catch(e){
database["@data_"+id] = void 0;
}
}
return ret;
}
}
$.mix( {
"@data": {},
acceptData: function( target ) {
return target && remitter.test(typeof target) && rtype.test(target.nodeType)
},
data: function( target, name, data ) { // 读写数据
return innerData(target, name, data)
},
_data: function(target,name,data){//仅内部调用
return innerData(target, name, data, true)
},
removeData: function(target, name){ //移除数据
return innerRemoveData(target, name);
},
_removeData: function(target, name){//仅内部调用
return innerRemoveData(target, name, true);
},
parseData: function(target, name, table, value){
var data, key = $.String.camelize(name),_eval
if(table && (key in table))
return table[key];
if(arguments.length != 4){
var attr = "data-" + name.replace( /([A-Z])/g, "-$1" ).toLowerCase();
value = target.getAttribute( attr );
}
if ( typeof value === "string") {//转换 /^(?:\{.*\}|null|false|true|NaN)$/
if(/^(?:\{.*\}|\[.*\]|null|false|true|NaN)$/.test(value) || +value + "" === value){
_eval = true
}
try {
data = _eval ? eval("0,"+ value ) : value
} catch( e ) {
data = value
}
if(table){
table[ key ] = data
}
}
return data;
},
//合并数据
mergeData: function( cur, src){
var oldData = $._data(src), curData = $._data(cur), events = oldData .events;
if(oldData && curData ){
$.Object.merge( curData , oldData );
if(events){
curData .events = [];
for (var i = 0, item ; item = events[i++]; ) {
$.event.bind( cur, item );
}
}
}
}
});
});
/**
2011.9.27 uniqueID改为uniqueNumber 简化data与removeData
2011.9.28 添加$._data处理内部数据
2011.10.21 强化mergeData,可以拷贝事件
2012.1.31 简化$.Object.merge的调用
2012.4.5 修正mergeData BUG, 让$.data能获取HTML5 data-*
2012.5.2 $._db -> $["@data]
2012.5.21 抽象出validate私有方法
2012.9.29 对parseData的数据进行严格的验证后才转换
*/