Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Short syntax for simple modes #444

Closed
qfox opened this issue Apr 26, 2017 · 7 comments
Closed

Short syntax for simple modes #444

qfox opened this issue Apr 26, 2017 · 7 comments
Assignees
Milestone

Comments

@qfox
Copy link
Member

qfox commented Apr 26, 2017

Before:

block('a').tag()('span')

After:

block('a')({ tag: 'span' })

Before:

block('a')(
    tag()('span'),
    addMix()({ block: 'x' }),
    mods()({ mod: 'val' }),
    content()(function() {
        return ['Vobla: ', this.ctx.name];
    })
});

After:

block('a')({
    tag: 'span',
    addMix: { block: 'x' },
    mods: { mod: 'val' },
    content() {
        return ['Vobla: ', this.ctx.name];
    }
});

Complex cases gonna be like this.

Before:

block('a')(
    tag()('a'),
    match(function() { return this.bla }).tag()('span')
);

After:

block('a')(
    { tag: 'a' },
    match(function() { return this.bla })({
        tag: 'span'
    })
);

Some cases gonna be only in old syntax:

block('a').tag()(
    'span',
    match(function() { })('a')
)
@miripiruni
Copy link
Contributor

miripiruni commented Apr 26, 2017

Before:

block('button').match(function() { return this._attachButton; })(
    tag()('span'),

    match(function() { return !!this._disabled; })
        .mix()({ mods: { disabled: 'yes' } }),

    def()(function() {
        return applyNext({
            _name: this.ctx.name || 'attachment',
            _id: this.ctx.id || this.generateId(),
            _textId: 'text' + this._id,
            _tabindex: this.ctx.tabindex
        });
    }),

    attrs()({}), // принудительно сбрасываем все атрибуты

    content()(function() {
        var p = applyNext(),
            a = {
                block: 'attach',
                elem: 'control',
                attrs: this.ctx.controlAttrs
            };

        return [p, a];
    }),

    elem('text').attrs()(function() {
        var a = applyNext() || {};
        a.id = this._textId;
        a['aria-hidden'] = true;
        return a;
    })
);

After:

block('button').match(function() { return this._attachButton; })(
    {
        tag: 'span',

        def: function() {
            return applyNext({
                _name: this.ctx.name || 'attachment',
                _id: this.ctx.id || this.generateId(),
                _textId: 'text' + this._id,
                _tabindex: this.ctx.tabindex
            });
        },

        attrs: {}, // принудительно сбрасываем все атрибуты

        content: function() {
            return [
                applyNext(),
                {
                    block: 'attach',
                    elem: 'control',
                    attrs: this.ctx.controlAttrs
                }
            ];
         }
    },

    elem('text')({
        attrs: function() {
            var a = applyNext() || {};
            a.id = this._textId;
            a['aria-hidden'] = true;
            return a;
        }),

    match(function() { return !!this._disabled; })({
        mix: { mods: { disabled: 'yes' } }
    })
);

@veged @zxqfox @arikon @tadatuta it’s real template from islands. Many combinations with compositions and chains, many objects literals. The result is very difficult to read and maintain.

@veged
Copy link
Member

veged commented Apr 26, 2017

The result is very difficult to read and maintain.

I can't agree ;-)

@qfox
Copy link
Member Author

qfox commented Apr 26, 2017

After (using all existing features in 8.x):

block('button').match(n => n._attachButton)(
    {
        tag: 'span',

        def() {
            return applyNext({
                _name: this.ctx.name || 'attachment',
                _id: this.ctx.id || this.generateId(),
                _textId: 'text' + this._id,
                _tabindex: this.ctx.tabindex
            });
        },

        attrs: {}, // принудительно сбрасываем все атрибуты

        appendContent() {
            return {
                    block: 'attach',
                    elem: 'control',
                    attrs: this.ctx.controlAttrs
            };
         }
    },

    elem('text')({
        addAttrs() {
            return {id: this._textId, 'aria-hidden': true};
        },

        match(n => n._disabled)({
            mix: { mods: { disabled: 'yes' } } // Is this `{block: ?, mods: {disabled: 'yes'}}` ?
        })
    })
);

It has much less noise.

@tenorok
Copy link

tenorok commented Nov 6, 2017

Such syntax is most clear in my opinion.
It's similar to the standard syntax of classes, object literal and i-bem in the end. It will be good highlighted in the editor and not to be confused developers. By the way, the possibility of different grouping of modes is unnecessary, because leads to many various syntax in project and big meaningless difference in commits.

@miripiruni
Copy link
Contributor

miripiruni commented Jan 9, 2018

I have been trying to program such shortcuts.

In general, my changes is:

  1. If last part of chain contain Object then try to iterate by it’s keys.
  2. All keys will be modes and values will be template bodies.

Example 1:

with shortcut:

block('b')({ tag: 'a' })

without shortcut:

block('b').tag()('a')

Example 2:

with shortcut:

block('b')( mix()('mixed'), { tag: 'a' } )

without shortcut:

block('b')( mix()('mixed'), tag()('a') )

Now we have such templates:

block('b').addMods()({ first: 'yes' })

Current version just add modifier first_yes to b. But according new rule, bem-xjst will be compile such template as mode first witch returns 'yes'.

There is two way to do:

  1. Prohibit object literals as returning value. And use object literals only as templates shortcuts. It’s major changes.
  2. Allow shortcuts only after block subpredicate. block('b')( { /* object literal here */ } ). According life experience that nobody uses anonymous mode (mode with out name). I’m not sure if it’s possible, but it looks more good than major changes.
  3. What other way?

@veged
Copy link
Member

veged commented Jan 11, 2018

I suggest 2'. — «Break chain on "mode" subpredicate and do not allow shortcuts further.»

@miripiruni miripiruni added this to the 8.9.0 milestone Feb 5, 2018
@miripiruni
Copy link
Contributor

  • bem-xjst@8.9.0
  • enb-bemxjst@8.10.0

And also already in Turbo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants