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

Global block ctx #210

Closed a-x- closed 8 years ago

a-x- commented 8 years ago

now i need proxy ctx in elems stupid example (Fixed) (DEMO):

block('b')(
    content()(function(){ return [
        { elem: 'elem1', foo1: this.ctx.foo1, foo2: this.ctx.foo2 }, 
        { elem: 'elem2', foo2: this.ctx.foo2, foo3: this.ctx.foo3 }
    ]}),

    elem('elem1').content()(function(){ return this.ctx.foo1 * this.ctx.foo2; }),

    elem('elem2').content()(function(){ return this.ctx.foo2 * this.ctx.foo3; })
)

In ideal world:

block('b')(
    content()(function(){ return [
        { elem: 'elem1' }, 
        { elem: 'elem2' }
    ]}),

    elem('elem1').content()(function(){ return this.blockCtx.foo1 * this.blockCtx.foo2; }),

    elem('elem2').content()(function(){ return this.blockCtx.foo2 * this.blockCtx.foo3; })
)

p.s. i understand, that global vars are evil, buuut...

miripiruni commented 8 years ago

You may be surprised, but here is live example of what you want.

a-x- commented 8 years ago

no, it's not

a-x- commented 8 years ago

i have to proxy all params into block's elems now

i want access all block's params in its elems via API, w/o proxing

a-x- commented 8 years ago

Это частый кейс, и проксирование обычно тривиально и только засоряет шаблоны

miripiruni commented 8 years ago

Can you show more examples?

miripiruni commented 8 years ago

@a-x- thank you for your feedback!

a-x- commented 8 years ago

Can you show more examples?

here: https://gist.github.com/a-x-/a380c1f4d5d0dc997e7a we set global ctx (this.params = this.ctx.params) and we use it around wide block elems in this file and in other block's bemhtmls