javascript - Polymer:dom-repeat 就像元素的局部变量

标签 javascript polymer polymer-1.0 web-component dom-repeat

我想创建一个控件,将数据转换成我需要的东西。

目前,我用一个全局变量来解决这个问题。代码看起来像这样: (小写功能只是为了简单地演示它。通常我想将它用于对象数组。例如,获取某些名称和 ID 的不同值)

<dom-module id="my-tolower">
  <script>
    "use strict";
    Polymer({
      is: 'my-tolower',
      properties: {
        input: {
          type: String,
          value: "",
        },
        output:{
          type: String,
          value: "",
          notify: true,
        }
      },
      observers:[
        "_inputChanged(input)",
      ],
      _inputChanged: function(newInput, oldInput){
        this.set("output", newInput.toLowerCase());
      }
    });
  </script>
</dom-module>

用法:

<my-tolower input="[[output.name]]" output="{{lower}}">[[lower]]</my-tolower>

如果我只使用变量 lower,这个解决方案效果很好只有一次。在 <dom-repeat> 里面,我遇到了问题。

我怎样才能轻松地创建一个只在 my-tolower 内部可用的自定义变量? ?与 Polymer 的 dom-repeat 完全一样是吗?

我查看了 Polymer's <dom-repeat> sources 的代码,但我不知道它是如何工作的。这在自定义元素中甚至可能吗?我需要创建自定义模板吗?


为了更好地解释我的问题,我添加了一个更大的示例来详细解释我的问题。

HTMLImports.whenReady(() => {
  Polymer({
    is: 'my-app',
    ready: function(){
      //In my real Problem this value comes from a websocket...
      this.devices = [{
        name: "PC 1",
        components: [
          {
            name: "HDD1",
            processors: [
              {
                type: "Processor1",
                usage: "Dont Know 1"
              },
              { type: "Processor1", usage: "DontKnow2"},
              { type: "Processor2", usage: "DontKnow3"}
            ]
          },
          {
            name: "Another Piece Of Hardware",
            processors: [
              {
                type: "Processor4",
                usage: "Dont Know 1"
              },
              { type: "Processor3", usage: "DontKnow2"},
              { type: "Processor4", usage: "DontKnow3"}
            ]
          }
        ]
      },
      {
        name: "PC 2",
        components: [
          {
            name: "My third piece of hardware",
            processors: [
              {
                type: "Processor1",
                usage: "Dont Know 1"
              },
              { type: "Processor2", usage: "DontKnow2"},
              { type: "Processor3", usage: "DontKnow3"}
            ]
          }
        ]
      }];
      //this.devices must not be changed!
    }
  });
  
  
  Polymer({
    is: 'my-distinct',
    properties: {
      inputs: {
        type: String
      },
      outputs:{
        computed: '_getDistincts(inputs, path)',
        notify: true
      },
      path: {
        type: String,
        value: ""
      }
    },
    _getDistincts(inputs, path){
      let result = [];
      
      for(let key in inputs){
        if(inputs.hasOwnProperty(key)){
          let x = inputs[key];
          if(path && path != ""){
            x = x[path];
          }
          
          if(result.indexOf(x) < 0){
            result.push(x);
          }
          else{
            //Already Exists
          }
        }
      }
      
      return result;
    }
    
  });
  
 
});
<head>
  <base href="https://polygit.org/polymer+1.8.1/components/">
  <script src="webcomponentsjs/webcomponents-lite.js"></script>
  <link rel="import" href="polymer/polymer.html">
</head>
<body>
  <my-app></my-app>
  As you can see, there is always "Processor1", "Processor2" and "Pocessor3" available although this is only the result of the last computers component. You can see the right result (but with duplicates) if you use the comment I made instead.
  
  <dom-module id="my-app">
    <template>
      <ul>
        <template is="dom-repeat" items="[[devices]]" as="device">
          <li>[[device.name]]
          <ul>
            <template is="dom-repeat" items="[[device.components]]" as="component">
              <li>[[component.name]]
                <ul>
                  <!-- This is my code with using distinct -->
                  <my-distinct inputs="[[component.processors]]" 
                      outputs="{{distinctProcessorNames}}" 
                      path="type">
                    <template is="dom-repeat" items="[[distinctProcessorNames]]" as="processorName">
                      <li>[[processorName]]
                        <!-- Here I could iterate over all processors (filtered) and list their usages-->
                      </li>
                    </template>
                  </my-distinct>
                  
                  <!-- This is my code without using distinct. -->
                  <!--template is="dom-repeat" items="[[component.processors]]" as="processor">
                    <li>[[processor.type]]
                      <ul>
                        <li>Used for [[processor.usage]]</li>
                      </ul>
                    </li>
                  </template-->
                </ul>
              </li>
            </template>
          </ul>
          </li>
        </template>
      </ul>
    </template>
  </dom-module>
</body>

Demo

最佳答案

您正在使用 2 个不同的 Polymer 自定义元素 <my-app><my-distinct> . 因此,您应该使用适当的 <dom-module> 声明第二个。声明:

<dom-module id="my-distinct">
    <template>
        <template is="dom-repeat" items="[[outputs]]" as="processorName">
            <li>[[processorName]]
        </template>
    </template>
</dom-module>

然后使用您的计算属性(基于属性值)outputs作为 items <template is="dom-repeat"> 的值.

下面的演示:

HTMLImports.whenReady(() => {
  Polymer({
    is: 'my-app',
    ready: function(){
      //In my real Problem this value comes from a websocket...
      this.devices = [{
        name: "PC 1",
        components: [
          {
            name: "HDD1",
            processors: [
              {
                type: "Processor1",
                usage: "Dont Know 1"
              },
              { type: "Processor1", usage: "DontKnow2"},
              { type: "Processor2", usage: "DontKnow3"}
            ]
          },
          {
            name: "Another Piece Of Hardware",
            processors: [
              {
                type: "Processor4",
                usage: "Dont Know 1"
              },
              { type: "Processor3", usage: "DontKnow2"},
              { type: "Processor4", usage: "DontKnow3"}
            ]
          }
        ]
      },
      {
        name: "PC 2",
        components: [
          {
            name: "My third piece of hardware",
            processors: [
              {
                type: "Processor1",
                usage: "Dont Know 1"
              },
              { type: "Processor2", usage: "DontKnow2"},
              { type: "Processor3", usage: "DontKnow3"}
            ]
          }
        ]
      }];
      //this.devices must not be changed!
    }
  });
  
  Polymer({
    is: 'my-distinct',
    properties: {
      inputs: {
        type: String
      },
      outputs:{
        computed: '_getDistincts(inputs, path)', notify: true
      },
      path: {
        type: String,
        value: ""
      }
    },
    _getDistincts(inputs, path){
      let result = [];
      
      for(let key in inputs){
        if(inputs.hasOwnProperty(key)){
          let x = inputs[key];
          if(path && path != ""){
            x = x[path];
          }
          
          if(result.indexOf(x) < 0){
            result.push(x);
          }
        }
      }
      //console.log( result )
      return result;
    } 
  });  
});
<head>
  <base href="https://polygit.org/polymer+1.8.1/components/">
  <script src="webcomponentsjs/webcomponents-lite.js"></script>
  <link rel="import" href="polymer/polymer.html">
</head>
<body>
  <my-app></my-app>
  
  <dom-module id="my-app">
    <template>
      <ul>
        <template is="dom-repeat" items="[[devices]]" as="device">
          <li>[[device.name]]
          <ul>
            <template is="dom-repeat" items="[[device.components]]" as="component">
              <li>[[component.name]]
                <ul>
                  <my-distinct inputs="[[component.processors]]" path="type">
                  </my-distinct>
                
                </ul>
              </li>
            </template>
          </ul>
          </li>
        </template>
      </ul>
    </template>
  </dom-module>

  <dom-module id="my-distinct">
    <template>
        <template is="dom-repeat" items="[[outputs]]" as="processorName">
            <li>[[processorName]]
        </template>
    </template>
  </dom-module>
</body>

关于javascript - Polymer:dom-repeat 就像元素的局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42852689/

相关文章:

javascript - 页面加载时的照片对齐应为网格

polymer - 如何以声明方式将禁用/检查的伪 bool 属性绑定(bind)到 bool 值?

javascript - 显示过时项目的 polymer 熨斗列表

javascript - 如何正确通知聚合物中 sibling 之间的数据更改

用于在文本区域中编码的 JavaScript 库(具有适当的缩进)

javascript - html5视频通过鼠标滚轮滚动播放

android - 有没有办法让 <paper-toolbar> 使用 Polymer Starter Kit 占据屏幕的整个宽度?

polymer - polymer 的静态特性

javascript - angularjs,browserify + 下划线

javascript - Polymer 元素中的 React.js 组件 : errors on every mouse/wheel/key event