Skip to content

Commit

Permalink
bem-xjst: isolate input bemjson (fix for #495)
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Jan 16, 2018
1 parent 36e253c commit 95373cc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/bemxjst/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,17 @@ BEMXJST.prototype.exportApply = function(exports) {
var ret = exports || {};

ret.apply = function(context) {
return self.run(context);
var arg;
if (Array.isArray(context)) {
arg = [].concat(context);
} else if (context === null) {
arg = null;
} else if (typeof context === 'object') {
arg = utils.extend({}, context);
} else {
arg = context;
}
return self.run(arg);
};

// Add templates at run time
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,
'first apply() call returns not expected value'

This comment has been minimized.

Copy link
@qfox

qfox Jan 23, 2018

Member

second?

);
});
});
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,
'first apply() call returns not expected value'
);
});

it('should use current context (with simple value)', function() {
test(function() {
Expand Down

0 comments on commit 95373cc

Please sign in to comment.