Skip to content

Commit 0605710

Browse files
committed
1. add data
1 parent 903caad commit 0605710

File tree

2 files changed

+128
-14
lines changed

2 files changed

+128
-14
lines changed

js/jing.js

Lines changed: 125 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
concat = arr.concat,
1414
push = arr.push,
1515
indexOf = arr.indexOf,// js 1.6新增加的方法
16+
version = '1.0',
1617
class2type = {},
1718
toString = class2type.toString,
1819
hasOwn = class2type.hasOwnProperty,
@@ -40,7 +41,7 @@
4041
}
4142

4243
$.fn = $.prototype = {
43-
jing : '1.0',
44+
jing : version,
4445
constructor : $,
4546
sort : arr.sort,
4647
push : push,
@@ -222,6 +223,8 @@
222223

223224
// 常用工具方法
224225
$.extend({
226+
// 非重复标志位
227+
expando : 'Jing' + (version + Math.random()).replace(/\D/g, ''),
225228
// 空函数快捷方式
226229
noop : function(){},
227230
isFunction : function(obj){
@@ -373,14 +376,18 @@
373376
for(i in key){
374377
_.access(elems, fn, i, key[i], true, emptyGet, raw);
375378
}
376-
} else if(value !== undefined){
379+
} else if(value !== undefined){ // 设置data,即set
377380
chainable = true;
378381

379382
if(!$.isFunction(value)){
380-
raw = true;
383+
raw = true; // value不是函数,则raw设为true
384+
}
385+
386+
if(bulk){ // data操作时必然true
387+
381388
}
382389

383-
if(fn){
390+
if(fn){ // fn存在,开始正式执行fn
384391
for(; i < length; i++){
385392
fn(elems[i], key, raw ? value : value.call(elems[i], i, fn(elems[i], key)));
386393
}
@@ -908,19 +915,130 @@
908915
}
909916
});
910917

918+
/**
919+
* 缓存内部方法
920+
* @param elem
921+
* @param name
922+
* @param data
923+
* @param pvt Jing内部参数
924+
* @returns {*}
925+
*/
911926
_.internalData = function(elem, name, data, pvt){
927+
var ret,
928+
thisCache,
929+
internalKey = $.expando,
930+
isNode = elem.nodeType,
931+
cache = isNode ? $.cache : elem,
932+
id = isNode ? elem[internalKey] : elem[internalKey] && internalKey;
933+
934+
if(!id){
935+
if(isNode){
936+
id = elem[internalKey] = arr.pop() || $.guid++;
937+
} else {
938+
id = internalKey;
939+
}
940+
}
941+
942+
if(!cache[id]){
943+
cache[id] = isNode ? {} : { toJSON : $.noop };
944+
}
945+
946+
thisCache = cache[id];
947+
948+
if(!pvt){
949+
if(!thisCache.data){
950+
thisCache.data = {};
951+
}
952+
953+
thisCache = thisCache.data;
954+
}
955+
956+
if(data !== undefined){
957+
thisCache[$.camelCase(name)] = data;
958+
}
912959

960+
if(typeof name === 'string'){
961+
ret = thisCache[name];
962+
963+
if(ret === null){
964+
ret = thisCache[$.camelCase(name)];
965+
}
966+
} else {
967+
ret = thisCache;
968+
}
969+
970+
return ret;
971+
}
972+
/**
973+
* 清除缓存数据
974+
* @param elem
975+
* @param name
976+
* @param pvt
977+
* @returns {*}
978+
*/
979+
_.internalRemoveData = function(elem, name, pvt){
980+
var ret;
981+
982+
return ret;
913983
}
914984
// 缓存
915985
$.extend({
916986
cache : {},
987+
data : function(elem, name, data){
988+
return _.internalData(elem, name, data);
989+
},
917990
_data : function(elem, name, data){
918-
991+
return _.internalData(elem, name, data, true);
992+
},
993+
removeData : function(elem, name){
994+
return _.internalRemoveData(elem, name);
995+
},
996+
_removeData : function(elem, name){
997+
return _.internalRemoveData(elem, name, true);
919998
}
920999
});
9211000

1001+
/**
1002+
* 获取html5 [data-key] 值
1003+
* @param elem
1004+
* @param key
1005+
* @param data
1006+
*/
1007+
_.dataAttr = function(elem, key, data){
1008+
1009+
}
9221010
$.fn.extend({
1011+
data : function(key, value){
1012+
var i,
1013+
name,
1014+
data,
1015+
elem = this[0],
1016+
attrs = elem && elem.attributes;
1017+
1018+
// key不存在,即为取值
1019+
if(key === undefined){
1020+
1021+
}
9231022

1023+
// key是对象,则递归set所有键值对
1024+
if(typeof key === 'object'){
1025+
return this.each(function(){
1026+
$.data(this, key);
1027+
});
1028+
}
1029+
1030+
return arguments.length > 1 ?
1031+
this.each(function(){
1032+
$.data(this, key, value)
1033+
}) :
1034+
elem ? $.data(elem, key) : undefined;
1035+
// elem ? _.dataAttr(elem, key, $.data(elem, key)) : undefined;
1036+
},
1037+
removeData : function(key){
1038+
return this.each(function(){
1039+
$.removeData(this, key);
1040+
});
1041+
}
9241042
});
9251043

9261044
}(window));
@@ -932,4 +1050,5 @@
9321050
// 2014-05-06 : 增加$.browser方法
9331051
// 2014-05-07 : 增加$().appendTo,$().prependTo等方法,增加$().html();
9341052
// 2014-05-08 : 增加$().css({ color : 'red' }), $(window).width,height(), $(document).width,height();
935-
// 2014-05-09 : 增加$().width()方法, event first;
1053+
// 2014-05-09 : 增加$().width()方法, event first;
1054+
// 2014-05-12 : 增加$().data(), $().removeData()方法

test.html

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,9 @@
1212
<div class="a">aa</div>
1313
<div class="a">bbb</div>
1414
<script type="text/javascript">
15-
console.log($(window).width());
16-
// console.log($('#test').width());
17-
// console.log($('#test').css('width'));
18-
// $('.a').css({ width : '100px', color : 'red'});
19-
console.log($(document).height());
20-
$('.a').css({ width :$(window).width(), height : $(document).height() });
21-
console.log($('.a'));
22-
15+
$('#test').data('name', 'jikey');
16+
var a = $('#test').data('name');
17+
console.log(a);
2318

2419
</script>
2520
</body>

0 commit comments

Comments
 (0)