bem / bem-xjst

bem-xjst (eXtensible JavaScript Templates): declarative template engine for the browser and server
https://bem.github.io/bem-xjst
Other
115 stars 47 forks source link

Arbitrary ctx for custom modes #73

Closed tadatuta closed 9 years ago

tadatuta commented 9 years ago

Is there still a way to do something like this: for BEMJSON

{ block: 'b1' }

and template

block('b1')(
    mode('custom')(function() {
        console.log(this.ctx); // expected 'trololo' but there's { block: 'b1' } instead
    }),
    def()(function() {
        return apply('custom', { ctx: 'trololo' })
    })
);

?

indutny commented 9 years ago

Fixed in 5a26b32, thanks!

tadatuta commented 9 years ago

I found out that there's still inconsistency in a way how it works in bem-xjst < 2.0: For BEMJSON { block: 'b1' } and templates

block('b1')(
    mode('custom')(function() {
        console.log(this.block); // b2
        return 'in b1';
    }),
    content()(function() {
        return apply('custom', { block: 'b2', trololo: 'blah' });
    })
);

block('b2').mode('custom')(function() {
    return 'in b2';
});

there will be <div class="b1">in b1</div> but <div class="b1">in b2</div> expected.

indutny commented 9 years ago

@tadatuta I think it was our initial plan to make it behave slightly different for performance reasons. What is your use case, is there any workaround for it?

tadatuta commented 9 years ago

Looks like there's no backward-compatible workarounds. Current use case is kinda global block which provides modes to be called from other blocks. If support for such possibility is performance heavy we'll think if we can drop compatibility in this case.

indutny commented 9 years ago

It could be implemented, but with the price of performance. Let's close this for one, and reopen and do minor version bump if there would be serious issues about it. Thanks!