From 91b5abea506516f9e65a642a1ab919ed8b68a997 Mon Sep 17 00:00:00 2001 From: miripiruni Date: Tue, 10 Jan 2017 18:49:36 +0300 Subject: [PATCH] BEMXJST: allow apply() inside match() (fix for #309) --- docs/en/5-templates-syntax.md | 2 ++ docs/ru/5-templates-syntax.md | 3 +++ lib/bemxjst/index.js | 11 ++++++++++- test/modes-match-test.js | 24 ++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/docs/en/5-templates-syntax.md b/docs/en/5-templates-syntax.md index 316975d6..61a33d8b 100644 --- a/docs/en/5-templates-syntax.md +++ b/docs/en/5-templates-syntax.md @@ -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: diff --git a/docs/ru/5-templates-syntax.md b/docs/ru/5-templates-syntax.md index 64f95df2..6113ca52 100644 --- a/docs/ru/5-templates-syntax.md +++ b/docs/ru/5-templates-syntax.md @@ -200,6 +200,9 @@ block('*').match(function() { return false; })( ); ``` +В теле функции-колбека `match` вы можете использовать [`apply()`](7-runtime.md#apply) для вызова +любого режима, относящегося к данному узлу. + ### Цепочки подпредикатов Подпредикаты можно выстраивать в цепочки: diff --git a/lib/bemxjst/index.js b/lib/bemxjst/index.js index d10bb35a..27131c91 100644 --- a/lib/bemxjst/index.js +++ b/lib/bemxjst/index.js @@ -472,7 +472,16 @@ BEMXJST.prototype.applyNext = function() { }; BEMXJST.prototype.applyMode = function(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; diff --git a/test/modes-match-test.js b/test/modes-match-test.js index 4391499b..6be2d25a 100644 --- a/test/modes-match-test.js +++ b/test/modes-match-test.js @@ -34,4 +34,28 @@ describe('Modes match', function() { block: 'bla' }, ''); }); + + 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' }, '
OK
'); + }); + + 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' }, '
OK
'); + }); });