Skip to content

Commit

Permalink
fixup! i-bem__dom: update _elemCache on findElem
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexej Yaroshevich committed Mar 12, 2015
1 parent 0b4b89d commit 0a49cc7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
7 changes: 4 additions & 3 deletions common.blocks/i-bem/__dom/i-bem__dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,8 @@ DOM = BEM.decl('i-bem__dom',/** @lends BEMDOM.prototype */{
res = findDomElem(ctx, selector);

// caching results if possible
(ctx === this.domElem) && keys.forEach(function(key, i) {
(ctx.length === 1 && this.domElem.length === 1 && ctx[0] === this.domElem[0]) &&
keys.forEach(function(key, i) {
var elem = this._elemCache[key] = isSingleName? res : res.filter('.' + key);
delete elem.prevObject; // drop link to original set after filtering
elem.__bemElemName = names[i];
Expand Down Expand Up @@ -778,8 +779,8 @@ DOM = BEM.decl('i-bem__dom',/** @lends BEMDOM.prototype */{
* @returns {jQuery} DOM elements
*/
_elem : function(name, modName, modVal) {
var key = this.__self.buildClass(name, modName, modVal);
return this._elemCache[key] || this.findElem(name, modName, modVal);
return this._elemCache[this.__self.buildClass(name, modName, modVal)] ||
this.findElem(name, modName, modVal);
},

/**
Expand Down
23 changes: 19 additions & 4 deletions common.blocks/i-bem/__dom/i-bem__dom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,14 +453,29 @@ describe('i-bem__dom', function() {
it('should update _elemCache after findElem call', function() {
rootBlock.elem('e1');
rootNode.html(BEMHTML.apply({
block : 'root',
elem : 'e1',
attrs : { id : '1.' }
}));
block : 'root',
elem : 'e1',
attrs : { id : '1.' }
}));
rootBlock.findElem('e1');

getElemIds(rootBlock.elem('e1')).should.be.eql(['1.']);
});

it('should update _elemCache only if ctx is the same', function() {
rootBlock.findElem($(rootNode), 'e1');
rootNode.html(BEMHTML.apply({
block : 'root',
elem : 'e1',
attrs : { id : '1.' }
}));

rootBlock.findElem($('.something-else'), 'e1');
getElemIds(rootBlock.elem('e1')).should.be.eql(['1', '2-1', '3-2-1']);

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

describe('DOM.init', function() {
Expand Down

0 comments on commit 0a49cc7

Please sign in to comment.