Skip to content

Commit

Permalink
i-bem__dom: implement detach() method
Browse files Browse the repository at this point in the history
port #1103 into v3
  • Loading branch information
Vladimir Varankin committed Jul 21, 2015
1 parent e685e88 commit c3fc629
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
16 changes: 12 additions & 4 deletions common.blocks/i-bem-dom/i-bem-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,11 +883,20 @@ bemDom = /** @exports */{
},

/**
* Destroys blocks on a fragment of the DOM tree
* Destroys blocks on a fragment of the DOM tree and destructs the tree
* @param {jQuery} ctx Root DOM node
* @param {Boolean} [excludeSelf=false] Exclude the main domElem
*/
destruct : function(ctx, excludeSelf) {
this.detach(ctx, excludeSelf, true);
},

/**
* Detaches blocks on a fragment of the DOM tree
* @param {jQuery} ctx Root DOM node
* @param {Boolean} [excludeSelf=false] Exclude the main domElem
*/
detach : function(ctx, excludeSelf, _destructDom) {
var _ctx;
storeDomNodeParents(_ctx = excludeSelf? ctx.children() : ctx);

Expand All @@ -905,9 +914,8 @@ bemDom = /** @exports */{
});

// NOTE: it was moved here as jquery events aren't triggered on detached DOM elements
excludeSelf?
ctx.empty() :
ctx.remove();
_destructDom &&
(excludeSelf? ctx.empty() : ctx.remove());

// flush parent nodes storage that has been filled above
domNodesToParents = {};
Expand Down
23 changes: 23 additions & 0 deletions common.blocks/i-bem-dom/i-bem-dom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,29 @@ describe('i-bem-dom', function() {
});
});

describe('bemDom.detach', function() {
it('should detach block but leave DOM node', function() {
var spy = sinon.spy();
bemDom.declBlock('block1', {
onSetMod : {
js : {
'' : spy
}
}
});

rootNode = createDomNode({
tag : 'div',
content : { block : 'block1', js : true }
});

bemDom.detach(rootNode.find('.block1'));

spy.should.have.been.calledOnce;
rootNode.find('.block1').length.should.be.equal(1);
});
});

describe('bemDom.destruct', function() {
var spy;
beforeEach(function() {
Expand Down

0 comments on commit c3fc629

Please sign in to comment.