简体   繁体   English

如何显示Polymer中父对象的信息?

[英]How to display information from a parent object in Polymer?

I am writing a Polymer element that collects information from an API and which it should distribute to child elements based on the result's object keys. 我正在编写一个Polymer元素,该元素从API收集信息,并应根据结果的对象键将其分发给子元素。

The my-parent element executes the ajax call. my-parent元素执行ajax调用。 The response if fetched in the response() function. 如果在response()函数中获取了response() ,则该response()

My question is this: how can I store the information received in a way, that I can distribute and display it to the child element? 我的问题是:如何以某种方式存储收到的信息,以便可以将其分发并显示给子元素?

App.html App.html

<my-parent collector="1">
    <h1>The Results</h1>
    <h3><my-child name="title"><!-- should output FOO --></my-child></h3>
    <h3><my-child name="description"><!-- should output BAR --></my-child></h3>
</my-parent>

my-parent.html my-parent.html

<dom-module id="my-parent">
  <template>
    <style>
      :host {
        display: block;
      }
    </style>
    <content></content>
    <iron-ajax auto url="//someurl/posts/[[collector]]" handle-as="json" last-response="{{response}}" on-response="onResponse" id="xhr"></iron-ajax>
 </template>
  <script>
      Polymer({
        is: 'my-parent',
        properties: {
          collector: {
            type: String,
            notify: true
          },
          response: {
            type: String
          }
        },
        onResponse: function(response){
          /* WHAT TO DO HERE? */
        }
      })
  </script>
</dom-module>

API result from //someurl/posts/1 API结果来自//someurl/posts/1

{
   "title": "FOO",
   "description": "BAR"
}

my-child.html my-child.html

<dom-module id="my-child">
  <template>
    <style>
      :host {
        display: block;
      }
    </style>
    {{itemes}}
  </template>
  <script>
      Polymer({
        is: 'my-child',
        properties: {
          itemes: {
            type: String,
            value: function(){
              return "what to do here?";
            }
          }
        },
        key: {
            type: String,
            notify: true
          }
      })
  </script>
</dom-module>

Since <my-child> is actually a light DOM child of <my-parent> and not part of <my-parent> 's local DOM (ie Shadow DOM), you'll have to use Polymer's DOM API . 由于<my-child>实际上是<my-parent>的轻型DOM子级,而不是<my-parent>的本地DOM(即Shadow DOM)的一部分,因此您必须使用Polymer的DOM API

In my-parent.html, remove the on-response="onResponse" attribute from <iron-ajax> and instead update your <script> to the following: 在my-parent.html中,从<iron-ajax>删除on-response="onResponse"属性,然后将<script>更新为以下内容:

Polymer({
  is: 'my-parent',
  properties: {
    collector: {
      type: String,
      notify: true
    },
    response: {
      type: Object,
      observer: '_handleResponse'
    }
  },
  _handleResponse: function(response) {
    Polymer.dom(this).querySelector('[name="title"]').itemes = response.title;
    Polymer.dom(this).querySelector('[name="description"]').itemes = response.description;
  }
})

and then my-child.html's <script> can be updated to the following: 然后my-child.html的<script>可以更新为以下内容:

Polymer({
  is: 'my-child',
  properties: {
    itemes: {
      type: String
    }
  }
})

Although that may not be exactly what you want, it shows you how to transfer data from a parent component to its light DOM children. 尽管这可能并不是您想要的,但它向您展示了如何将数据从父组件传输到其轻型DOM子代。 In this example, we're setting the itemes property of each <my-child> and that property is set to render its text value as a local DOM text node. 在此示例中,我们设置每个<my-child>itemes属性,并且将该属性设置为将其文本值呈现为本地DOM文本节点。

All of that being said, I'm not sure this approach will work with the Shadow DOM v1 spec (there you may need to have the nodes be direct children of and then perhaps have the as a light DOM child or local/shadow DOM child), but for Polymer 1.x using Shady DOM, it'll do the trick. 综上所述,我不确定该方法是否适用于Shadow DOM v1规范(您可能需要将节点作为的直接子代,然后再将其作为轻型DOM子代或本地/阴影DOM子代。 ),但对于使用Shady DOM的Polymer 1.x,它将解决问题。

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

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