Skip to content

Commit

Permalink
Merge pull request #1103 from /issues/1102@v2
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 7de5aa3 + 86410fa commit de08e7a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
28 changes: 22 additions & 6 deletions common.blocks/i-bem/__dom/i-bem__dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,18 +985,19 @@ DOM = BEM.decl('i-bem__dom',/** @lends BEMDOM.prototype */{
},

/**
* 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;
if(excludeSelf) {
storeDomNodeParents(_ctx = ctx.children());
ctx.empty();
destructDom && ctx.empty();
} else {
storeDomNodeParents(_ctx = ctx);
ctx.remove();
destructDom && ctx.remove();
}

reverse.call(findDomElem(_ctx, BEM_SELECTOR)).each(function(_, domNode) {
Expand All @@ -1011,9 +1012,24 @@ DOM = BEM.decl('i-bem__dom',/** @lends BEMDOM.prototype */{
});
delete domElemToParams[identify(domNode)];
});
},

/**
* 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);
},

// flush parent nodes storage that has been filled above
domNodesToParents = {};
/**
* Detaches blocks on a fragment of the DOM tree without destructing DOM tree
* @param {jQuery} ctx Root DOM node
* @param {Boolean} [excludeSelf=false] Exclude the main domElem
*/
detach : function(ctx, excludeSelf) {
this._destruct(ctx, excludeSelf);
},

/**
Expand Down
29 changes: 29 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 @@ -635,6 +635,35 @@ describe('i-bem__dom', function() {
});
});

describe('DOM.detach', function() {
it('should detach block and leave DOM node', function() {
var spy = sinon.spy();
DOM.decl('block2', {
onSetMod : {
js : {
'' : spy
}
}
});

var rootNode = DOM.init($(BEMHTML.apply({
block : 'block1',
content : {
block : 'block2',
js : true
}
})));

DOM.detach(rootNode.bem('block1').findBlockInside('block2').domElem);

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

delete DOM.blocks['block1'];
delete DOM.blocks['block2'];
});
});

describe('DOM.update', function() {
it('should update tree', function() {
var spyBlock1Destructed = sinon.spy(),
Expand Down

0 comments on commit de08e7a

Please sign in to comment.