Skip to content

Commit

Permalink
Merge pull request #609 from bem/issued/608@v2
Browse files Browse the repository at this point in the history
i-bem__dom: base `live` is not called in derived block (close #608)
  • Loading branch information
dfilatov committed Jul 28, 2014
2 parents 40c7e47 + d5de780 commit 1d1b607
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
12 changes: 10 additions & 2 deletions common.blocks/i-bem/__dom/i-bem__dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -925,9 +925,17 @@ var DOM = BEM.decl('i-bem__dom',/** @lends BEMDOM.prototype */{
if('live' in this) {
var noLive = typeof res === 'undefined';

if(noLive ^ heedLive) {
if(noLive ^ heedLive) { // should be opposite to each other
res = this.live() !== false;
this.live = functions.noop;

var blockName = this.getName(),
origLive = this.live;

this.live = function() {
return this.getName() === blockName?
res :
origLive.apply(this, arguments);
};
}
}

Expand Down
82 changes: 82 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 @@ -811,6 +811,88 @@ describe('i-bem__dom', function() {
});
});

describe('live', function() {
it('should properly use live of base block', function() {
var spy1 = sinon.spy(),
spy2 = sinon.spy();

DOM.decl('block1', {}, { live : spy1 });
DOM.decl({ block : 'block2', baseBlock : 'block1' }, {}, {
live : function() {
this.__base.apply(this, arguments);
spy2();
}
});

DOM.init(BEMHTML.apply([
{ block : 'block1', js : true },
{ block : 'block2', js : true }
]));

spy1.should.have.been.calledTwice;
spy2.should.have.been.calledOnce;

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

it('should properly use live after adding a new block', function() {
var spy1 = sinon.spy(),
spy2 = sinon.spy();

DOM.decl('block1', {}, { live : spy1 });

DOM.init(BEMHTML.apply({ block : 'block1', js : true }));

spy1.should.have.been.calledOnce;

DOM.decl({ block : 'block2', baseBlock : 'block1' }, {}, {
live : function() {
this.__base.apply(this, arguments);
spy2();
}
});

spy2.should.not.have.been.called;

DOM.init(BEMHTML.apply({ block : 'block2', js : true }));

spy1.should.have.been.calledTwice;
spy2.should.have.been.calledOnce;

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

it('should properly use live after adding declaration', function() {
var spy1 = sinon.spy(),
spy2 = sinon.spy();

DOM.decl('block1', {}, { live : spy1 });

DOM.init(BEMHTML.apply({ block : 'block1', js : true }));

spy1.should.have.been.calledOnce;

DOM.decl('block1', {}, {
live : function() {
this.__base.apply(this, arguments);
spy2();
}
});

spy1.should.have.been.calledOnce;
spy2.should.have.been.calledOnce;

DOM.init(BEMHTML.apply({ block : 'block1', js : true }));

spy1.should.have.been.calledOnce;
spy2.should.have.been.calledOnce;

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

describe('liveInitOnBlockInsideEvent', function() {
it('should init and call handler on live initialization', function() {
var spyInit = sinon.spy(),
Expand Down
7 changes: 5 additions & 2 deletions common.blocks/i-bem/i-bem.vanilla.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,13 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ {
});
}

decl.block === baseBlock.getName()?
if(decl.block === baseBlock.getName()) {
// makes a new "live" if the old one was already executed
(block = inherit.self(baseBlocks, props, staticProps))._processLive(true) :
(block = inherit.self(baseBlocks, props, staticProps))._processLive(true);
} else {
(block = blocks[decl.block] = inherit(baseBlocks, props, staticProps))._name = decl.block;
delete block._liveInitable;
}

return block;
},
Expand Down

0 comments on commit 1d1b607

Please sign in to comment.