Skip to content

Commit

Permalink
Merge pull request #843 from /issues/583@v3
Browse files Browse the repository at this point in the history
i-bem-dom: update _elemCache on findElem
  • Loading branch information
Vyacheslav Aristov committed May 14, 2015
2 parents 786834c + 6d6332c commit 57ace5f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 15 deletions.
8 changes: 4 additions & 4 deletions common.blocks/i-bem-dom/i-bem-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ var BemDomEntity = inherit(/** @lends BemDomEntity.prototype */{
*/
findChildElems : function(Elem, strictMode) {
var res = this._findEntities('find', Elem);
return strictMode? this._filterFindElemResults(res) : res;
return strictMode? this._filterFindElemResults(res) : this._elemsCache[buildElemKey(Elem)] = res;
},

/**
Expand All @@ -379,7 +379,7 @@ var BemDomEntity = inherit(/** @lends BemDomEntity.prototype */{
findChildElem : function(Elem, strictMode) {
return strictMode?
this._filterFindElemResults(this._findEntities('find', Elem))[0] :
this._findEntities('find', Elem, true);
this._elemCache[buildElemKey(Elem)] = this._findEntities('find', Elem, true);
},

/**
Expand Down Expand Up @@ -1190,7 +1190,7 @@ var Block = inherit([BEM.Block, BemDomEntity], /** @lends Block.prototype */{
var key = buildElemKey(Elem);
return key in this._elemsCache?
this._elemsCache[key] :
this._elemsCache[key] = this.findChildElems(Elem);
this.findChildElems(Elem);
},

/**
Expand All @@ -1204,7 +1204,7 @@ var Block = inherit([BEM.Block, BemDomEntity], /** @lends Block.prototype */{
// NOTE: can use this._elemsCache but it's too rare case
return key in this._elemCache?
this._elemCache[key] :
this._elemCache[key] = this.findChildElem(Elem);
this.findChildElem(Elem);
},

/**
Expand Down
90 changes: 79 additions & 11 deletions common.blocks/i-bem-dom/i-bem-dom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ describe('i-bem-dom', function() {
});
});
});

describe('find*Block(s)', function() {
var rootBlock,
B1Block, B3Block, B4Block, B5Block;

beforeEach(function() {
var RootBlock = BEMDOM.declBlock('root');
B1Block = BEMDOM.declBlock('b1');
Expand Down Expand Up @@ -222,7 +222,7 @@ describe('i-bem-dom', function() {

rootBlock = rootNode.bem(RootBlock);
});

describe('findChildBlocks', function() {
it('should return instances of Block founded by class', function() {
rootBlock.findChildBlocks(B1Block).forEach(function(block) {
Expand All @@ -233,12 +233,12 @@ describe('i-bem-dom', function() {
it('should find all blocks by block class', function() {
getEntityIds(rootBlock.findChildBlocks(B1Block)).should.be.eql(['1', '2', '3', '4']);
});

it('should find all blocks by block class, modName and modVal', function() {
getEntityIds(rootBlock.findChildBlocks({ block : B1Block, modName : 'm1', modVal : 'v1' }))
.should.be.eql(['2']);
});

it('should find all blocks by block class and boolean mod', function() {
getEntityIds(rootBlock.findChildBlocks({ block : B1Block, modName : 'm1', modVal : true }))
.should.be.eql(['4']);
Expand All @@ -249,7 +249,7 @@ describe('i-bem-dom', function() {
.should.be.eql(['4']);
});
});

describe('findChildBlock', function() {
it('should return instance of Block found by class', function() {
rootBlock.findChildBlock(B1Block).should.be.instanceOf(B1Block);
Expand Down Expand Up @@ -283,7 +283,7 @@ describe('i-bem-dom', function() {
.should.be.equal('4');
});
});

describe('findParentBlocks', function() {
var leafBlock;

Expand All @@ -294,12 +294,12 @@ describe('i-bem-dom', function() {
it('should find all ancestor blocks by block class', function() {
getEntityIds(leafBlock.findParentBlocks(B1Block)).should.be.eql(['3', '1']);
});

it('should find all ancestor blocks by block class, modName and modVal', function() {
getEntityIds(leafBlock.findParentBlocks({ block : B1Block, modName : 'm1', modVal : 'v2' }))
.should.be.eql(['3']);
});

it('should find all ancestor blocks by block class and boolean mod', function() {
getEntityIds(leafBlock.findParentBlocks({ block : B3Block, modName : 'm1', modVal : true }))
.should.be.eql(['5']);
Expand Down Expand Up @@ -639,18 +639,47 @@ describe('i-bem-dom', function() {
describe('elem(s)', function() {
var b1Block,
B1E1Elem,
B1E2Elem,
B1E3Elem,
spy;

beforeEach(function() {
var B1Block = BEMDOM.declBlock('b1');

B1E1Elem = BEMDOM.declElem('b1', 'e1');
B1E2Elem = BEMDOM.declElem('b1', 'e2');
B1E3Elem = BEMDOM.declElem('b1', 'e3');

rootNode = createDomNode({
block : 'b1',
content : [
{ elem : 'e1', js : { id : 1 } },
{ elem : 'e1', elemMods : { m1 : 'v1' }, js : { id : 2 } }
{ elem : 'e1', elemMods : { m1 : 'v1' }, js : { id : 2 } },
{
elem : 'e2', js : { id : 3 },
content : [
{
elem : 'e1', js : { id : 4 },
elemMods : { inner : 'no' }
},
{
elem : 'e3', js : { id : 5 },
elemMods : { inner : 'no', bool : true }
}
]
},
{
elem : 'e3', js : { id : 6 },
content : {
elem : 'e2', js : { id : 7 },
elemMods : { inner : 'yes', bool : true },
content : {
elem : 'e1', js : { id : 8 },
elemMods : { inner : 'yes', bool : true }
}
}
},
{ elem : 'e2', js : { id : 9 }, elemMods : { bool : true } }
]
});

Expand All @@ -668,7 +697,7 @@ describe('i-bem-dom', function() {

it('should find all elems by elem class', function() {
getEntityIds(b1Block.elems(B1E1Elem))
.should.be.eql([1, 2]);
.should.be.eql([1, 2, 4, 8]);
});

it('should find all elems by elem class modName and modVal', function() {
Expand Down Expand Up @@ -713,6 +742,45 @@ describe('i-bem-dom', function() {
b1Block.elems({ elem : B1E1Elem, modName : 'm2', modVal : 'v1' });
spy.should.be.calledThrice;
});

it('should cache found elems with findChildElems', function() {
// warm cache
[B1E1Elem, B1E2Elem, B1E3Elem].forEach(function (Elem) {
b1Block.findChildElems({ elem : Elem });
b1Block.findChildElems({ elem : Elem, modName : 'inner', modVal : 'no' });
b1Block.findChildElems({ elem : Elem, modName : 'inner', modVal : 'yes' });
b1Block.findChildElems({ elem : Elem, modName : 'bool', modVal : true });
});

// reset spy. it shouldn't be called inside `elems`
spy.restore();
spy = sinon.spy(b1Block, 'findChildElems');

getEntityIds(b1Block.elems('e1')).should.be.eql([1, 2, 4, 8]);
getEntityIds(b1Block.elems('e2')).should.be.eql([3, 7, 9]);
getEntityIds(b1Block.elems('e3')).should.be.eql([5, 6]);
getEntityIds(b1Block.elems({ elem : 'e1', modName : 'inner', modVal : 'yes' })).should.be.eql([8]);
getEntityIds(b1Block.elems({ elem : 'e2', modName : 'inner', modVal : 'yes' })).should.be.eql([7]);
getEntityIds(b1Block.elems({ elem : 'e3', modName : 'inner', modVal : 'no' })).should.be.eql([5]);
getEntityIds(b1Block.elems({ elem : 'e1', modName : 'bool', modVal : true })).should.be.eql([8]);
getEntityIds(b1Block.elems({ elem : 'e2', modName : 'bool', modVal : true })).should.be.eql([7, 9]);
getEntityIds(b1Block.elems({ elem : 'e3', modName : 'bool', modVal : true })).should.be.eql([5]);

b1Block.findChildElems.called.should.be.false;
b1Block.findChildElems.restore();
});

it('should update _elemsCache on findChildElem call', function() {
b1Block.elems('e1');
rootNode.html(BEMHTML.apply({
block : 'b1',
elem : 'e1',
js : { id : 10 }
}));
b1Block.findChildElems('e1');

getEntityIds(b1Block.elems('e1')).should.be.eql([10]);
});
});

describe('elem', function() {
Expand Down

0 comments on commit 57ace5f

Please sign in to comment.