Skip to content

Commit

Permalink
BEMXJST: allow apply() inside match() (fix for #309)
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Jan 10, 2017
1 parent c54f2f7 commit 8131592
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/en/5-templates-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ block('*').match(function() { return false; })(
);
```

Inside `match` callback function you can use [`apply()`](7-runtime.md#apply) to call any mode from this block.

### Subpredicate chains

Subpredicates can be arranged as chains:
Expand Down
3 changes: 3 additions & 0 deletions docs/ru/5-templates-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ block('*').match(function() { return false; })(
);
```

В теле функции-колбека `match` вы можете использовать [`apply()`](7-runtime.md#apply) для вызова
любого режима, относящегося к данному узлу.

### Цепочки подпредикатов

Подпредикаты можно выстраивать в цепочки:
Expand Down
11 changes: 10 additions & 1 deletion lib/bemxjst/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,16 @@ BEMXJST.prototype.applyNext = function applyNext() {
};

BEMXJST.prototype.applyMode = function applyMode(mode, changes) {
var match = this.match.entity.rest[mode];
var key;
var match = this.match;

if (!match) {
var key = this.classBuilder.build(this.context.block, this.context.elem);
match = this.entities[key].rest[mode];
} else {
match = this.match.entity.rest[mode];
}

if (!match) {
if (mode === 'mods')
return this.context.mods;
Expand Down
24 changes: 24 additions & 0 deletions test/modes-match-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,28 @@ describe('Modes match', function() {
block: 'bla'
}, '<span class="bla"></span>');
});

it('should work with apply() inside match()',
function() {
test(function() {
block('b')(
mode('test')(function() { return true; }),

match(function() { return apply('test'); })
.content()(function() { return 'OK'; })
);
}, { block: 'b' }, '<div class="b">OK</div>');
});

it('should work with apply() with changes inside match()',
function() {
test(function() {
block('b')(
mode('test')(function() { return this.changes; }),

match(function() { return apply('test', { changes: true }); })
.content()(function() { return 'OK'; })
);
}, { block: 'b' }, '<div class="b">OK</div>');
});
});

0 comments on commit 8131592

Please sign in to comment.