Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bem-xjst: clean cached flags for wrap() and extend() (fix for #495) #496

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/bemxjst/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ function BEMXJST(options) {
// Current match
this.match = null;

this._wraped = [];
this._extended = [];

// Create new Context constructor for overriding prototype
this.contextConstructor = function ContextChild(bemxjst) {
Context.call(this, bemxjst);
Expand Down Expand Up @@ -311,12 +314,26 @@ BEMXJST.prototype._run = function(context) {
return this.runOne(context);
};

BEMXJST.prototype.cleanWrapFlags = function(field) {
var cached = this[field];

if (cached.length === 0)
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;
Expand Down
10 changes: 8 additions & 2 deletions lib/bemxjst/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ function MatchWrap(template) {
MatchWrap.prototype.exec = function(context) {
var res = this.wrap !== context.ctx;
this.wrap = context.ctx;

context._bemxjst._wraped.push(this);

return res;
};

Expand All @@ -54,8 +57,11 @@ 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;

context._bemxjst._extended.push(this);

return res;
};

Expand Down
20 changes: 20 additions & 0 deletions test/modes-extend-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,24 @@ describe('Modes extend', function() {
}, { block: 'b', foo: 'This is' },
'<div class="b">This is ContextChild</div>');
});

it('should work with several apply() calls', function() {
var bemjson = { block: 'b1' };
var expected = '<div class="b1">42</div>';
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'
);
});
});
24 changes: 24 additions & 0 deletions test/modes-wrap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ describe('Modes wrap', function() {
}, { block: 'b1' }, '<div class="b1"></div>');
});

it('should work with several apply() calls', function() {
var bemjson = { block: 'b1' };
var expected = '<div class="b2"><div class="b1"></div></div>';
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() {
Expand Down