Skip to content

Commit 134ea10

Browse files
enuremadrobby
authored andcommitted
add ability to set data attributes via .data(obj)
1 parent 8f0298d commit 134ea10

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,4 @@ Detailed test output is logged in the JavaScript console of your browser.
183183
[issues]: https://github.com/madrobby/zepto/issues
184184
[mkd]: http://github.github.com/github-flavored-markdown/
185185
[evidence.js]: https://github.com/tobie/Evidence
186-
[optional]: http://mislav.uniqpath.com/2010/05/semicolons/
186+
[optional]: http://mislav.uniqpath.com/2010/05/semicolons/

src/data.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,18 @@
2222
return store
2323
}
2424

25+
function setDataAttributes(node,obj) {
26+
node.each(function() {
27+
for (var k in obj) {
28+
setData(this, k, obj[k]);
29+
}
30+
});
31+
};
32+
2533
$.fn.data = function(name, value) {
2634
return value === undefined ?
27-
this.length == 0 ? undefined : getData(this[0], name) :
35+
this.length == 0 ? undefined :
36+
$.isObject(name) ? setDataAttributes(this, name) : getData(this[0], name) :
2837
this.each(function(idx){
2938
setData(this, name, $.isFunction(value) ?
3039
value.call(this, idx, getData(this, name)) : value)

test/data.html

+43
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ <h1>Zepto data() tests</h1>
1818
<div id="fixtures">
1919
<div id="data_attr" data-one="uno" data-two="due"></div>
2020
<div id="data_full" data-mode="awesome"></div>
21+
<div id="data_obj" data-mode="awesome"></div>
2122
<ol id="data_list">
2223
<li data-category="arts"></li>
2324
<li data-category="science"></li>
2425
</ol>
26+
<ol id="data_list2">
27+
<li></li>
28+
<li></li>
29+
</ol>
2530
</div>
2631

2732
<script>
@@ -89,7 +94,45 @@ <h1>Zepto data() tests</h1>
8994
t.assertEqual('ni', all.two)
9095
t.assertEqual('Kurosawa', all.person.name)
9196
t.assertUndefined(all.mode)
97+
},
98+
99+
testSettingDataWithObj: function(t){
100+
var el = $('#data_obj')
101+
102+
el.data({
103+
'foo': 'bar',
104+
'answer': 42,
105+
'color': 'blue'
106+
})
107+
108+
var all = el.data()
109+
110+
t.assertEqual(all.answer, 42)
111+
t.assertEqual(all.color, 'blue')
112+
t.assertEqual(all.foo, 'bar')
113+
114+
el.data('foo', 'baz')
115+
116+
t.assertEqual(all.foo, 'baz')
117+
t.assertEqual(all.answer, 42)
118+
},
119+
120+
testSettingDataWithObjOnManyElements: function(t){
121+
var items = $('#data_list2 li')
122+
123+
items.data({
124+
'foo': 'bar',
125+
'answer': 42,
126+
'color': 'purple'
127+
})
128+
129+
var values = items.map(function(){ return $(this).data('foo') })
130+
t.assertEqual('bar, bar', values.join(', '))
131+
132+
var values2 = items.map(function(){ return $(this).data('answer') })
133+
t.assertEqual('42, 42', values2.join(', '))
92134
}
135+
93136
})
94137
</script>
95138
</body>

0 commit comments

Comments
 (0)