Skip to content

Commit

Permalink
add removeData() method to data module
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Apr 8, 2012
1 parent 4cf3020 commit cfe8b33
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@
// set value on all elements
this.each(function(){ setData(this, name, value) })
}

$.fn.removeData = function(names) {
if (typeof names == 'string') names = names.split(/\s+/)
return this.each(function(){
var id = this[exp], store = id && data[id]
if (store) $.each(names, function(){ delete store[camelize(this)] })
})
}
})(Zepto)
27 changes: 27 additions & 0 deletions test/data.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,33 @@ <h1>Zepto data() tests</h1>
t.assertFalse(el.data('bool'))
},

testRemoveData: function(t){
var el = $('<div data-foo=bar />')

el.data('foo', 'bam').data('bar', 'baz')
el.removeData('foo').removeData('bar')
t.assertEqual('bar', el.data('foo'))
t.assertUndefined(el.data('bar'))

el.data('uno', 'one').data('due', 'two')
el.removeData('uno due')
t.assertUndefined(el.data('uno'))
t.assertUndefined(el.data('due'))

el.data('one', 1).data('twoThree', 23)
el.removeData(['one', 'two-three'])
t.assertUndefined(el.data('one'))
t.assertUndefined(el.data('twoThree'))
},

testRemoveDataNoop: function(t){
var empty = $(),
vanilla = $('<div />')

t.assertIdentical(empty, empty.removeData('foo'))
t.assertIdentical(vanilla, vanilla.removeData('foo'))
},

testSettingDataWithObj: function(t){
var el = $('#data_obj')

Expand Down

0 comments on commit cfe8b33

Please sign in to comment.