Skip to content

Commit

Permalink
Merge pull request #1104 from /issues/1102@v3
Browse files Browse the repository at this point in the history
i-bem-dom: implement detach() method
  • Loading branch information
Vladimir Varankin committed Jul 21, 2015
2 parents e685e88 + 4db0501 commit 02e4290
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
28 changes: 23 additions & 5 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,12 @@ bemDom = /** @exports */{
},

/**
* Destroys blocks on a fragment of the DOM tree
* @param {jQuery} ctx Root DOM node
* @param {Boolean} [excludeSelf=false] Exclude the main domElem
* @param {Boolean} [destructDom=false] Remove DOM node during destruction
* @private
*/
destruct : function(ctx, excludeSelf) {
_destruct : function(ctx, excludeSelf, destructDom) {
var _ctx;
storeDomNodeParents(_ctx = excludeSelf? ctx.children() : ctx);

Expand All @@ -905,14 +906,31 @@ 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 = {};
},

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

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

/**
* Replaces a fragment of the DOM tree inside the context, destroying old blocks and intializing new ones
* @param {jQuery} ctx Root DOM node
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 02e4290

Please sign in to comment.