diff --git a/lib/bemxjst/index.js b/lib/bemxjst/index.js index d54cb181..77890243 100644 --- a/lib/bemxjst/index.js +++ b/lib/bemxjst/index.js @@ -311,12 +311,26 @@ BEMXJST.prototype._run = function(context) { return this.runOne(context); }; +BEMXJST.prototype.cleanWrapFlags = function(field) { + var cached = this[field]; + + if (!Array.isArray(cached)) + return; + + for (var i = 0; i < cached.length; i++) + cached[i].wrap = null; +}; + BEMXJST.prototype.run = function(json) { var match = this.match; var context = this.context; var depth = this.depth; this.match = null; + + this.cleanWrapFlags('_extended'); + this.cleanWrapFlags('_wraped'); + this.context = new this.contextConstructor(this); this.canFlush = this.context._flush !== null; this.depth = 0; diff --git a/lib/bemxjst/match.js b/lib/bemxjst/match.js index 718e64b1..03abd8eb 100644 --- a/lib/bemxjst/match.js +++ b/lib/bemxjst/match.js @@ -45,6 +45,11 @@ function MatchWrap(template) { MatchWrap.prototype.exec = function(context) { var res = this.wrap !== context.ctx; this.wrap = context.ctx; + + if (!Array.isArray(context._bemxjst._wraped)) + context._bemxjst._wraped = []; + context._bemxjst._wraped.push(this); + return res; }; @@ -54,8 +59,13 @@ function MatchExtend(template) { } MatchExtend.prototype.exec = function(context) { - var res = this.ext !== context.ctx; - this.ext = context.ctx; + var res = this.wrap !== context.ctx; + this.wrap = context.ctx; + + if (!Array.isArray(context._bemxjst._extended)) + context._bemxjst._extended = []; + context._bemxjst._extended.push(this); + return res; }; diff --git a/test/modes-extend-test.js b/test/modes-extend-test.js index a291ad65..237532ba 100644 --- a/test/modes-extend-test.js +++ b/test/modes-extend-test.js @@ -81,4 +81,24 @@ describe('Modes extend', function() { }, { block: 'b', foo: 'This is' }, '
This is ContextChild
'); }); + + it('should work with several apply() calls', function() { + var bemjson = { block: 'b1' }; + var expected = '
42
'; + var tmpl = fixtures.compile(function() { + block('b1').extend()({ 'ctx.content': 42 }); + }); + + assert.equal( + tmpl.apply(bemjson), + expected, + 'first apply() call returns not expected value' + ); + + assert.equal( + tmpl.apply(bemjson), + expected, + 'second apply() call returns not expected value' + ); + }); }); diff --git a/test/modes-wrap-test.js b/test/modes-wrap-test.js index 834cf5d2..536103a2 100644 --- a/test/modes-wrap-test.js +++ b/test/modes-wrap-test.js @@ -58,6 +58,30 @@ describe('Modes wrap', function() { }, { block: 'b1' }, '
'); }); + it('should work with several apply() calls', function() { + var bemjson = { block: 'b1' }; + var expected = '
'; + var tmpl = fixtures.compile(function() { + block('b1').wrap()(function() { + return { + block: 'b2', + content: this.ctx + }; + }); + }); + + assert.equal( + tmpl.apply(bemjson), + expected, + 'first apply() call returns not expected value' + ); + + assert.equal( + tmpl.apply(bemjson), + expected, + 'second apply() call returns not expected value' + ); + }); it('should use current context (with simple value)', function() { test(function() {