Skip to content

Commit

Permalink
context: support this.reapply()
Browse files Browse the repository at this point in the history
Fix: #42
  • Loading branch information
indutny committed May 5, 2015
1 parent 6d26216 commit dbed080
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
8 changes: 7 additions & 1 deletion lib/bemhtml/runtime/context.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var utils = require('./utils');

function Context() {
function Context(bemhtml) {
this._bemhtml = bemhtml;

this.ctx = null;
this.block = '';

Expand Down Expand Up @@ -50,3 +52,7 @@ Context.prototype.generateId = function generateId() {
this._uniq = utils.getUniq();
return this._uniq;
};

Context.prototype.reapply = function reapply(ctx) {
return this._bemhtml.run(ctx);
};
31 changes: 20 additions & 11 deletions lib/bemhtml/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ BEMHTML.prototype.compile = function compile(templates) {
var self = this;

function applyCtx() {
return self.run(self.context.ctx);
return self._run(self.context.ctx);
}

function applyCtxWrap(changes) {
Expand Down Expand Up @@ -127,12 +127,7 @@ BEMHTML.prototype.transformEntities = function transformEntities(entities) {
return entities;
};

BEMHTML.prototype.reset = function reset() {
this.match = null;
this.context = new Context();
};

BEMHTML.prototype.run = function run(context) {
BEMHTML.prototype._run = function _run(context) {
var res;
if (!context)
res = this.runEmpty();
Expand All @@ -145,6 +140,21 @@ BEMHTML.prototype.run = function run(context) {
return res;
};

BEMHTML.prototype.run = function run(json) {
var match = this.match;
var context = this.context;

this.match = null;
this.context = new Context(this);
var res = this._run(json);

this.match = match;
this.context = context;

return res;
};


BEMHTML.prototype.runEmpty = function runEmpty() {
this.context._listLength--;
return '';
Expand All @@ -165,7 +175,7 @@ BEMHTML.prototype.runMany = function runMany(arr) {
context._notNewList = true;

for (var i = 0; i < arr.length; i++)
out += this.run(arr[i]);
out += this._run(arr[i]);

if (!prevNotNewList)
context.position = prevPos;
Expand Down Expand Up @@ -507,7 +517,7 @@ BEMHTML.prototype.renderContent = function renderContent(content, isBEM) {
context._listLength = 1;
}

var res = this.run(content);
var res = this._run(content);

if (isBEM) {
context.position = oldPos;
Expand All @@ -528,7 +538,7 @@ BEMHTML.prototype.renderNoTag = function renderNoTag(context,

// TODO(indutny): skip apply next flags
if (content || content === 0)
return this.run(content);
return this._run(content);
return '';
};

Expand Down Expand Up @@ -579,7 +589,6 @@ BEMHTML.prototype.applyMode = function applyMode(mode) {
BEMHTML.prototype.exportApply = function exportApply(exports) {
var self = this;
exports.apply = function apply(context) {
self.reset();
return self.run(context || this);
};

Expand Down
13 changes: 13 additions & 0 deletions test/runtime-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ describe('BEMHTML compiler/Runtime', function() {
}, { block: 'b1' }, '<div class="b1"><div class="b1__e1"></div></div>');
});

it('should support this.reapply()', function() {
test(function() {
block('b1').content()(function() {
this.wtf = 'fail';
return this.reapply({ block: 'b2' });
});

block('b2').content()(function() {
return this.wtf || 'ok';
});
}, { block: 'b1' }, '<div class="b1"><div class="b2">ok</div></div>');
});

describe('mods', function() {
it('should lazily define mods', function() {
test(function() {
Expand Down

0 comments on commit dbed080

Please sign in to comment.