简体   繁体   English

在Polymer 1.0中记录库/混合/行为元素

[英]Documenting library/mixin/behaviors elements in Polymer 1.0

I had a Polymer 0.5 element that I mainly used as a library which I injected into other elements using mixins. 我有一个Polymer 0.5元素,我主要将其用作库,然后使用mixins注入到其他元素中。 I formatted it as followed and all of the JSDoc notation showed up in index.html : 我将其格式化为以下格式,所有JSDoc符号均显示在index.html

<polymer-element name="easy-search-lib" attributes="">

  <template>

    <content></content>

  </template>

  <script>

    var EasySearch = {

      /**
       * Returns whether the given domain matches search.
       *
       * @method matches
       * @param {String} query String being searched for.
       * @param {String} text Text being searched within.
       * @return {Boolean} Returns if there is a match.
       */
      matches: function(query, text){
        query = this.getQuery(query);
        return query.test(text);
      }

      //...

    };

    Polymer(Polymer.mixin({

      /**
       * Convenience function for testing, binds EasySearch to Polymer element.
       *
       * @attribute EasySearch
       * @type object
       */
      EasySearch: EasySearch

    }, EasySearch));

  </script>

</polymer-element>

Polymer 1.0 replaced mixins with behaviors and gives the following example: Polymer 1.0用行为替换了mixins并给出了以下示例:

<script>
    HighlightBehavior = {

      properties: {
        isHighlighted: {
          type: Boolean,
          value: false,
          notify: true,
          observer: '_highlightChanged'
        }
      },

      listeners: {
        click: '_toggleHighlight'
      },

      created: function() {
        console.log('Highlighting for ', this, + 'enabled!');
      },

      _toggleHighlight: function() {
        this.isHighlighted = !this.isHighlighted;
      },

      _highlightChanged: function(value) {
        this.toggleClass('highlighted', value);
      }

    };
</script>

Note that there is no <dom-module id="highlight-behavior"> nor Polymer({...}) element declaration. 请注意,既没有<dom-module id="highlight-behavior">也没有Polymer({...})元素声明。

I followed this example exactly, but nothing showed up when I visited index.html. 我完全按照此示例进行操作,但是在访问index.html时没有任何显示。 So I tried emulating what I had done for 0.5: 所以我尝试模拟我为0.5做的事情:

<script>

  var EasySearchLib = {

    EasySearch : {

      /**
       * Returns whether the given domain matches search.
       *
       * @method matches
       * @param {String} query String being searched for.
       * @param {String} text Text being searched within.
       * @return {Boolean} Returns if there is a match.
       */
      matches: function(query, text){
        query = this.getQuery(query);
        return query.test(text);
      }

      //...
     }
  };

  Polymer({

    is: 'easy-search-lib',

    properties: {

      /**
       * `fancy` indicates that the element should don a monocle and tophat,
       * while checking its pocket watch.
       */
      EasySearchLib: EasySearchLib
    }

  });

</script>

I also tried declaring variations of the attribute assignment ( EasySearch: EasySearchLib.EasySearch ) and throwing in a behaviors: [EasySearchLib] but nothing shows up in the documentation. 我还尝试声明属性分配的变体( EasySearch: EasySearchLib.EasySearch )并EasySearch: EasySearchLib.EasySearch以下behaviors: [EasySearchLib]但文档中未显示任何内容。

What's the best way of getting documentation for behaviors/libraries to show up when visiting index.html? 在访问index.html时获取行为/库文档的最佳方式是什么?

Put this before your behavior object definition: 将其放在您的行为对象定义之前:

/** @polymerBehavior */
var MyBehavior = {};

If you are putting multiple behaviors in one object, you will probably want to use this: 如果要将多个行为放在一个对象中,则可能需要使用以下方法:

var EasySearchLib = {

  /** @polymerBehavior EasySearchLib.EasySearch */
  EasySearch: {}
}

Alternatively, I am in a similar situation where I define my namespace object beforehand with: 另外,在类似的情况下,我预先定义了我的命名空间对象:

var EasySearchLib = EasySearchLib || {};

At the head of the document, then define the behavior with: 在文档的开头,然后使用以下命令定义行为:

/** @polymerBehavior */
EasySearchLib.EasySearch = {};

This lets the parser infer the name of the behavior pretty easily. 这使解析器可以轻松推断行为的名称。 It also allows you to split the individual behaviors into separate files and attach them in any order. 它还允许您将单个行为拆分为单独的文件,并以任意顺序附加它们。

Additional documentation: http://polymerelements.github.io/style-guide/#behaviors 附加文档: http : //polymerelements.github.io/style-guide/#behaviors

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

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