Skip to content

Commit

Permalink
i-bem__dom: fix dropElemCache logic (close #1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfilatov committed Jun 9, 2015
1 parent 8c54a15 commit 20b5c56
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
16 changes: 9 additions & 7 deletions common.blocks/i-bem/__dom/i-bem__dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,17 +739,19 @@ DOM = BEM.decl('i-bem__dom',/** @lends BEMDOM.prototype */{
names = names.split(' ');

var _self = this.__self,
modPostfix = buildModPostfix(modName, modVal),
selectors = [],
keys = names.map(function(name) {
return _self.buildClass(name, modName, modVal);
selectors.push(_self.buildSelector(name, modName, modVal));
return name + modPostfix;
}),
isSingleName = keys.length === 1,
selector = '.' + (isSingleName? keys[0] : keys.join(',.')),
res = findDomElem(ctx, selector);
res = findDomElem(ctx, selectors.join(','));

// caching results if possible
(ctx.length === 1 && this.domElem.length === 1 && ctx[0] === this.domElem[0]) &&
keys.forEach(function(key, i) {
(this._elemCache[key] = isSingleName? res : res.filter('.' + key))
ctx === this.domElem &&
selectors.forEach(function(selector, i) {
(this._elemCache[keys[i]] = isSingleName? res : res.filter(selector))
.__bemElemName = names[i];
}, this);

Expand Down Expand Up @@ -778,7 +780,7 @@ DOM = BEM.decl('i-bem__dom',/** @lends BEMDOM.prototype */{
* @returns {jQuery} DOM elements
*/
_elem : function(name, modName, modVal) {
return this._elemCache[this.__self.buildClass(name, modName, modVal)] ||
return this._elemCache[name + buildModPostfix(modName, modVal)] ||
this.findElem(name, modName, modVal);
},

Expand Down
26 changes: 24 additions & 2 deletions common.blocks/i-bem/__dom/i-bem__dom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ describe('i-bem__dom', function() {
});

it('should update _elemCache only if ctx is the same', function() {
rootBlock.findElem($(rootNode), 'e1');
rootBlock.findElem(rootBlock.domElem, 'e1');
rootNode.html(BEMHTML.apply({
block : 'root',
elem : 'e1',
Expand All @@ -473,11 +473,33 @@ describe('i-bem__dom', function() {
rootBlock.findElem($('.something-else'), 'e1');
getElemIds(rootBlock.elem('e1')).should.be.eql(['1', '2-1', '3-2-1']);

rootBlock.findElem($(rootNode), 'e1');
rootBlock.findElem(rootBlock.domElem, 'e1');
getElemIds(rootBlock.elem('e1')).should.be.eql(['1.']);
});
});

describe('drop elem cache', function() {
var block;
beforeEach(function() {
block = $(BEMHTML.apply({ block : 'b1', content : { elem : 'e1', mods : { m1 : 'v1' } } })).bem('b1');
});

afterEach(function() {
DOM.destruct(block.domElem);
delete DOM.blocks['b1'];
});

it('should properly drop elem cache', function() {
sinon.spy(block, 'findElem');

block.elem('e1', 'm1', 'v1');
block.dropElemCache('e1', 'm1', 'v1');
block.elem('e1', 'm1', 'v1');

block.findElem.should.have.been.calledTwice;
});
});

describe('DOM.init', function() {
it('should init block', function() {
var spy = sinon.spy();
Expand Down

0 comments on commit 20b5c56

Please sign in to comment.