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

Change api: this.param instead of this.ctx.param #211

Closed a-x- closed 8 years ago

a-x- commented 8 years ago

there is crazy non-realistic idea...

move service stuff in something like this._, because its are used rarely. and move block's params from this.ctx to this, because its are used frequently

miripiruni commented 8 years ago

@a-x- what kind of stuff you named “service stuff”?

Are you read documentation? In this section explained in detail about semantics of this fields and this.ctx.

Feel free to reopen this discussion if something not clear.

a-x- commented 8 years ago

Thank you, I haven't seen above linked documentation page earlier

Recently i dramatically improve my xjst understanding, and i know about this.ctx.mods and this.mods, for example today.

I'll read doc too soon.

a-x- commented 8 years ago

any way, this.ctx.someParam, is uses more frequently than this.something as i see.

Am I right?

a-x- commented 8 years ago

for example in our project:

ALL_THIS=$(ag -Gbemhtml 'this\.' | wc -l);
THIS_CTX=$(ag -Gbemhtml 'this\.ctx' | wc -l);
node -p "(($ALL_THIS - $THIS_CTX) / $ALL_THIS * 100).toFixed(2) + '%'"

this.something not this.ctx.someParam:

9.31%

this.ctx.someParam: ~ 90%

miripiruni commented 8 years ago

What do not you like? That this.ctx.prop is longer than this.prop? We can’t merge this.ctx fields and this fields because it’s different objects.

miripiruni commented 8 years ago

@a-x- we can’t change API as you suggest in issue title. Because of this case:

{
    block: 'example',
    ctx: { something: 'wrong' },
    reapply: 'oh, my',
    isSimple: 'etc'
}
block('example').def()(function() {
    var html = this.reapply({ block: 'reapply-me' });
    …
    return this.isSimple('test');
})

But I have good news for you: take a look at https://github.com/bem/bem-xjst/pull/200

After merge you can write templates:

// BEMJSON: { block: 'b', prop: 'bar' }
// Template: 
block('b').def()(function(_, ctx) {
    return ctx.prop; // 'bar'
});
a-x- commented 8 years ago

cool