Skip to content

Commit

Permalink
implement removeData method
Browse files Browse the repository at this point in the history
  • Loading branch information
kupriyanenko committed Jun 22, 2014
1 parent 90105a0 commit ca4c773
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,29 @@ fn.data = function(key, value) {

return this;
};

fn.removeData = function(key) {
var i = 0,
length = this.length,
jdata, dataset;

for (; i < length; i++) {
jdata = this[i].jdata;
dataset = this[i].dataset;

if (key) {
jdata && jdata[key] && delete jdata[key];
delete dataset[key];
} else {
for (key in jdata) {
delete jdata[key];
}

for (key in dataset) {
delete dataset[key];
}
}
}

return this;
};
32 changes: 32 additions & 0 deletions test/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,36 @@ describe('jBone Attributes', function() {
expect(a.data('third')).be.eql(obj);
});

it('removeData(key)', function() {
var a = jBone('<div>');

a.data('name', 'John');
a.data('fn', function() {});
expect(a.data('name')).be.eql('John');

a
.removeData('name')
.removeData('fn');
expect(a.data('name')).be.eql(undefined);
expect(a.data('fn')).be.eql(undefined);
});

it('removeData()', function() {
var a = jBone('<div>');

a.data('name', 'John');
a.data('fn', function() {});
expect(a.data('name')).be.eql('John');

a.removeData();
expect(a.data('name')).be.eql(undefined);
expect(a.data('fn')).be.eql(undefined);
});

it('removeData(key) and removeData() witn not defined keys', function() {
var a = jBone('<div>');
a.removeData('name');
a.removeData();
});

});

0 comments on commit ca4c773

Please sign in to comment.