聚合物共享组分 属性 结构

polymer share component property structure

如果我有下面的polymer元素,我如何让这个元素的用户知道dataSource中对象的结构。即如果我必须声明并共享 Employee 函数,以便他们可以 new Employee() 并创建一个数组。共享公共数据对象的聚合物方式是什么?有些似乎暗示行为,但这不是组件的行为,这是一个具有属性但没有行为的数据对象。

<dom-module id="employee-list"> 
  <template>
          <ul>
              <template is="dom-repeat" items="{{ dataSource }}">
                  <li>
                     <span>{{item.firstName}}</span>
                  </li>
              </template>
          </ul>          
  </template>
  <script>
  Polymer({
    is: 'employee-list',
    properties: {
        dataSource: {
            type: Array,
            value: [{ firstName: "First 1", lastName: "Last 1" }]
        }
    }
  });
  </script>
</dom-module>

您的用户将像这样使用您的元素

<employee-list datasource="[[usersEmployeeList]]"></employee-list>

您需要在 属性 上的 jsdoc 注释中告诉他们数据源的格式。我不知道正确的 jsdoc 注释的格式,但看看现有的聚合物元素的方式。

使用行为!尽管名称具有误导性,但您可以使用行为来共享属性。

A behavior is an object that looks similar to a typical Polymer prototype. A behavior can define lifecycle callbacks, declared properties, default attributes, observers, and listeners.

https://www.polymer-project.org/1.0/docs/devguide/behaviors.html

JS Bin 示例:http://jsbin.com/vuqumo/edit?html,output