Skip to content

Commit

Permalink
remove all related listners after removing jBone object
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey.Kupriyanenko committed Apr 22, 2014
1 parent aed6040 commit 5d8c4fd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ fn.off = function(event, fn) {
return;
}

// remove all events
if (!event && events) {
return keys(events).forEach(function(eventType) {
events[eventType].forEach(function(e, index) {
removeListener(events, eventType, index, el, e);
});
});
}

event.split(" ").forEach(function(event) {
eventType = event.split(".")[0];
namespace = event.split(".").splice(1).join(".");
Expand Down
5 changes: 4 additions & 1 deletion src/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ fn.remove = function() {
length = this.length,
el;

// remove all listners
this.off();

for (; i < length; i++) {
el = this[i];

delete jBone._cache.events[el.jid];
// remove data and nodes
delete el.jdata;
el.parentNode && el.parentNode.removeChild(el);
}
Expand Down
4 changes: 2 additions & 2 deletions test/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe('jBone Event', function() {
});

it('trigger() order', function() {
var markup = jBone('<div><div><p><span><b>b</b></span></p></div></div>'),
var markup = jBone('<div><div><p><span><b></b></span></p></div></div>'),
path = '';

jBone('#app').append(markup);
Expand All @@ -263,7 +263,7 @@ describe('jBone Event', function() {

markup.find('b').trigger('click');

expect(path).be.eql('b span p div div ');
expect(path).be.eql('b p div div ');
});

it('trigger() on element without handlers', function() {
Expand Down
14 changes: 14 additions & 0 deletions test/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,18 @@ describe('jBone Manipulation', function() {

expect(a).to.be.a(jBone);
});

it('remove() should remoe all event listners', function() {
var a = jBone('<a>'),
i = 0;
a.on('click', function() {
i++;
});

a.remove();

a.trigger('click');

expect(i).to.be.eql(0);
});
});

0 comments on commit 5d8c4fd

Please sign in to comment.