Polymer 1.0:有什么方法可以将 'layout' 用作属性而不是 CSS class 或在 class 属性中使用属性序列化?

Polymer 1.0: Is there any way to use 'layout' as an attribute instead of as a CSS class or using Attribute serialization in the class attribute?

我已经在 polymer 0.5 中构建了我的应用程序。

现在我已经更新到 polymer 1.0.

对于响应式布局,我在 Polymer 0.5 中使用了布局属性自定义逻辑的布局属性。

查看下面的代码:

<template is="auto-binding">
<core-media-query query="max-width: 767px" queryMatches="{{smallScreen}}"></core-media-query>
<section layout vertical?="{{smallScreen}}" horizontal?="{{!smallScreen}}">
    <section flex four?="{{!smallScreen}}" auto?="{{smallScreen}}">
        <paper-input-decorator label="First name" floatingLabel>
            <input type="text" is="paper-input" id="fname" name="fname">
        </paper-input-decorator>
    </section>
    <section flex four?="{{!smallScreen}}" auto?="{{smallScreen}}">
        <paper-input-decorator label="Last name" floatingLabel>
            <input type="text" is="paper-input" id="lname" name="lname">
        </paper-input-decorator>
    </section>
</section>
</template>

现在在 polymer 1.0 中引入了一个元素 "iron-layout-flex",所有迹象表明现在我们必须使用 classes“.layout”、“.horizo​​ntal”、“.vertical”而不是属性“?很迷惑,我应该如何按照我的布局属性逻辑进行调整。

所以,我的问题是,有什么方法可以将 'layout' 用作属性而不是 CSS class 或在 class 中使用属性序列化属性?

好吧,理论上您可以将属性转换为新布局 css 属性。像这样的东西(未经测试,所以不能保证这真的有效)

<style is="custom-style">
  html /deep/ [layout][vertical] {
    @apply(--layout-vertical);
  }

  html /deep/ [layout][horizontal] {
    @apply(--layout-horizontal);
  }

  html /deep/ [flex][four] {
    @apply(--layout-flex-4);
  }

  html /deep/ [flex][auto] {
    @apply(--layout-flex-auto);
  }
</style>

从 Polymer 1.2 开始,现在可以使用 compound binding 参见 Polymer Blog

也就是说,您现在应该可以执行以下操作:

<div class="someOtherClass [[addClassIf('horizontal', smallScreen)]] [[addClassIf('vertical', !smallScreen)]]" >
</div>

..
smallScreen: {
   type: Boolean,
   value: false,
}
..

addClassIf: function(classToAdd, shouldAdd)
{
   if(shouldAdd)
      return classToAdd;
}

既然 compound binding 可行,可能还有其他(更好的)方法可以做到这一点。但这显示了如何完成它的概念。