简体   繁体   English

在聚合物2中应用JS Mixins的行为

[英]Applying Behaviors with JS Mixins in Polymer 2

I want a custom element I'm defining to have the Polymer.IronScrollTargetBehavior in Polymer 2. 我想要一个我定义的自定义元素,在Polymer 2中使用Polymer.IronScrollTargetBehavior

In Polymer 1, this can be done by adding it to the behaviors array: 在Polymer 1中,可以通过将其添加到behaviors数组来完成:

Polymer({
    is: 'my-element',
    behaviors: [Polymer.IronScrollTargetBehavior]
});

In the Polymer 2 upgrade guide , it says that you should: 在Polymer 2 升级指南中 ,它说你应该:

Implement "behaviors" as mixins that return class expressions . 将“behavior”实现为返回类表达式的mixins

In the linked article, it explains how you can use the following syntax for mixins: 在链接的文章中,它解释了如何对mixins使用以下语法:

let MyMixin = (superclass) => class extends superclass {  
    foo() {
        console.log('foo from MyMixin');
    }
};

class MyClass extends MyMixin(MyBaseClass) {  
    /* ... */
}

I mostly get what's going on here (although I find the mixin syntax difficult to wrap my mind around), and I can get sample code to work. 我主要得到的是这里发生的事情(尽管我发现mixin语法难以理解),我可以获得示例代码。

What I haven't been able to do is apply this concept to Polymer.IronScrollTargetBehavior , and create a mixin for it. 我无法做的是将此概念应用于Polymer.IronScrollTargetBehavior ,并为其创建一个mixin。 Since that behavior is already defined as an object, I don't know where to fit it in. 由于该行为已经被定义为一个对象,我不知道在哪里适合它。

So, how do I implement the proper mixin in this scenario, or if I'm on the wrong path, how to I apply one of the defined Polymer behaviors to my custom element in Polymer 2? 那么,如何在这种情况下实现正确的mixin,或者如果我走错路径,如何将一个已定义的Polymer行为应用于Polymer 2中的自定义元素?

You can use the Polymer 2 hybrid behaviours as mixins by extending Polymer.mixinBehaviors(behaviors, klass) where 您可以通过扩展Polymer.mixinBehaviors(behaviors, klass)将Polymer 2混合行为用作mixins
- behaviors is the Behavior object or array of behaviors - behaviors是Behavior对象或行为数组
- klass is the Element class. - klass是Element类。

ie

<dom-module id="element-name">
  <template><!-- ... --></template>
  <script>
    class MyElement extends Polymer.mixinBehaviors([MyBehavior, MyBehavior2], Polymer.Element) {
     static get is() { return 'element-name' }
     /* ... */
    }
    customElements.define('element-name', MyElement);
  </script>
</dom-module>

For more detailed information search the Polymer source code for mixinBehaviors method: polymer/lib/legacy/class.html 有关更多详细信息,请搜索mixinBehaviors方法的Polymer源代码: polymer/lib/legacy/class.html

worth reading: https://www.polymer-project.org/2.0/docs/upgrade#mixins 值得一读: https//www.polymer-project.org/2.0/docs/upgrade#mixins

Polymer 2.0 has a compatibility layer that still supports the old Polymer function syntax. Polymer 2.0具有兼容层,仍然支持旧的Polymer函数语法。 Most of the 2.0 preview elements, if not all, still retain the old syntax. 大多数2.0预览元素(如果不是全部)仍保留旧语法。 The breaking changes are mostly in the dom-module markup. 突破性变化主要在dom-module标记中。

If you are composing new elements, it is recommended you switch over to the class based syntax. 如果要编写新元素,建议您切换到基于类的语法。 If however you are porting 1.0 elements to 2.0 and those elements rely on Polymer behaviors, I don't think you much choice at this juncture but to retain the old syntax. 但是,如果你将1.0元素移植到2.0并且那些元素依赖于Polymer行为,那么我认为你在这个时刻并没有多少选择,而是保留旧的语法。

In the class-based syntax you can fluently simulate Element multiple inheritance of class mixins with something like this 在基于类的语法中,您可以使用类似的方法流畅地模拟类mixin的Element多重继承

    let Mixin = (superclass) => new MixinBuilder(superclass);
    class MixinBuilder {  
        constructor(superclass) {
          this.superclass = superclass;
        }

        with(...mixins) { 
         return mixins.reduce((c, mixin) => mixin(c), this.superclass);
        }
    }

    const MyMixin = subclass => class extends subclass {
      _test(){

      }
    }

    const MyMixinTwo = subclass => class extends subclass {
      _testTwo(){

      }
    }

    class MyElement extends Mixin(Polymer.Element).with(MyMixin,MyMixin2) {
       static get is() { return 'my-element' }
    }  

You can separate the MixinBuilder into its own file and then reference it as an Html Import dependency whenever composing elements that use mixins. 您可以将MixinBuilder分离到自己的文件中,然后在组成使用mixins的元素时将其作为Html Import依赖项引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM