Skip to content

Commit

Permalink
Fix unbind on block destruct
Browse files Browse the repository at this point in the history
  • Loading branch information
belozer committed Jun 22, 2018
1 parent 9ab08cc commit a978ac7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe('BEM events', function() {
});

describe('block instance events', function() {
var block2_1, block2_2;
var block1, block2_1, block2_2, block3_1, block3_2;
beforeEach(function() {
Block1 = bemDom.declBlock('block1', {
onSetMod : {
Expand All @@ -222,14 +222,27 @@ describe('BEM events', function() {
});

Block2 = bemDom.declBlock('block2');
Block3 = bemDom.declBlock('block3');

block1 = initDom({
block : 'block1',
content : [
{ block : 'block2' },
{ block : 'block2' }
]
}).bem(Block1);
var dom = initDom([
{
block : 'block1',
content : [
{ block : 'block2' },
{ block : 'block2' }
]
},
{ block : 'block3' },
{ block : 'block3' },
]);

block1 = dom.eq(0).bem(Block1);

block3_1 = dom.eq(1).bem(Block3);
block3_2 = dom.eq(2).bem(Block3);

block1._events(block3_1).on('click', spy6);
block1._events(block3_2).on('click', spy7);
});

it('should properly bind handlers', function() {
Expand Down Expand Up @@ -267,6 +280,15 @@ describe('BEM events', function() {
spy1.should.not.have.been.called;
spy3.should.have.been.called;
});

it('should properly unbind all handlers on block destruct', function() {
bemDom.destruct(block1.domElem);
block3_1._emit('click');
block3_2._emit('click');

spy6.should.have.not.been.called;
spy7.should.have.not.been.called;
});
});

describe('nested blocks events', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,15 +677,23 @@ describe('DOM events', function() {
this._domEvents(rootNode.find('div').addBack())
.on('dblclick', data, wrapSpy(spy3))
.once('dblclick', spy4);

this._domEvents(rootNode[1])
.on('click', spy5);
}
}
}
});
rootNode = createDomNode({
content : {
content : { block : 'block', tag : 'p' }
rootNode = createDomNode([
{
content : {
content : { block : 'block', tag : 'p' }
}
},
{
content : ''
}
});
]);
block1 = rootNode.find(Block1._buildSelector()).bem(Block1);
});

Expand Down Expand Up @@ -748,6 +756,7 @@ describe('DOM events', function() {

spy1.should.not.have.been.called;
spy2.should.not.have.been.called;
spy5.should.not.have.been.called;
});
});
});
Expand Down
7 changes: 4 additions & 3 deletions common.blocks/i-bem-dom/__events/i-bem-dom__events.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,10 @@ var undef,
ctxStorage = eventStorage[ctxId] = {};
if(isBindToInstance) {
ctx._events().on({ modName : 'js', modVal : '' }, function() {
params.bindToArbitraryDomElem && ctxStorage[storageKey] &&
ctxStorage[storageKey].un();
delete ctxStorage[ctxId];
objects.each(ctxStorage, function(eventManager) {
eventManager.un();
});
delete eventStorage[ctxId];
});
}
}
Expand Down

0 comments on commit a978ac7

Please sign in to comment.