【问题标题】:Why doesn't element catch event when using dispatchEvent from sibling polymer element?为什么在使用来自同级聚合物元素的 dispatchEvent 时元素不捕获事件?
【发布时间】:2019-01-03 22:00:07
【问题描述】:

我的 UI 中有以下聚合物元素,它有两个聚合物元素,baseline-policy-create 和 baseline-policy-domain-ajax。

<dom-module id="baseline-policies-tab">
  <template>
    <style include="dfw-styles">
      :host {
        display: block;
      }
    </style>
    <baseline-policy-create></baseline-policy-create>
    <baseline-policy-domain-ajax></baseline-policy-domain-ajax>
  </template>

  <script>

    class BaselinePoliciesTab extends Polymer.Element {
      static get is() {
        return 'baseline-policies-tab';
      }

      static get properties() {
      }
    }
    customElements.define(BaselinePoliciesTab.is, BaselinePoliciesTab);

  </script>
</dom-module>

在我的baseline-policy-create 元素中,我有一个下拉按钮,可以让我选择“包”或“订阅”。当我选择其中一个时,我调度了一个 CustomEvent,它应该触发我在baseline-policy-domain-ajax 中注册的监听器。下面是baseline-policy-create的代码:

<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<link rel="import" href="../shared-styles.html">
<link rel="import" href="../../bower_components/dfw-styles/dfw-styles.html">
<link rel="import" href="../../bower_components/paper-button/paper-button.html">
<link rel="import" href="../../bower_components/paper-menu-button/paper-menu-button.html">
<link rel="import" href="../../bower_components/paper-listbox/paper-listbox.html">

<dom-module id="baseline-policy-create">
  <template>
    <style include="dfw-styles">
      :host {
        display: block;
      }
      .top-button{
        float : right;
      }
    </style>

    <div class="outer-buttons">
      <paper-menu-button horizontal-align="right" no-overlap="true" no-animations class="top-button">
        <paper-button slot="dropdown-trigger" class="dropdown-trigger create-button btn-primary">
          <iron-icon icon="menu"></iron-icon>
          <span>Create Baseline Policy</span>
        </paper-button>
        <paper-listbox slot="dropdown-content" class="dropdown-content">
          <template is="dom-repeat" items="{{_domains}}">
            <paper-item on-tap="_getDomainSchema">[[item.label]]</paper-item>
          </template>
        </paper-listbox>
      </paper-menu-button>
    </div>
  </template>
  <script>
    class BaselinePolicyCreate extends Polymer.Element {
      static get is() {
        return 'baseline-policy-create';
      }

      static get properties() {
        return {
          _domains: {
            type: Array,
            value: [{'name':'Package', 'label':'Package'},
                    {'name':'Subscription', 'label':'Subscription'}] //TODO: is it possible to get these values from an API source
          }
        }
      }

      _getDomainSchema(evt) {
        console.info("Get the schema for the following domain:", evt.target.innerText);
        // fire event here to trigger ajax call in baseline-policy-domain-ajax
        this.dispatchEvent(new CustomEvent('domain-ajax',{detail : evt.target.innerText}));
      }
    }
    customElements.define(BaselinePolicyCreate.is, BaselinePolicyCreate);
  </script>
</dom-module>

这是baseline-policy-domain-ajax的代码:

<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<link rel="import" href="../../bower_components/polymer/polymer.html">


<dom-module id="baseline-policy-domain-ajax">
  <template>
    <style></style>
    <iron-ajax
      id = "getSchema"
      auto = false
      url="<removed-url-for-confidentiality>"
      params='{"domain" : "Package"}'
      handle-as="json"
      on-response="_handleResponse"
      debounce-duration="300">
      </iron-ajax>
  </template>

  <script>
    class BaselinePolicyDomainAjax extends Polymer.Element {
      static get is() { return 'baseline-policy-domain-ajax'; }

      //static get properties() {  }

      //static get observers() {  }

      connectedCallback(){
        super.connectedCallback();
        console.log("ajax element is attached! Register listeners");
        document.addEventListener('domain-ajax',this._editPage.bind(this),true);
      }

      _handleResponse(event, request) {
        console.log ('Handle Response');
        var response = request.response;
        console.log(response);
      }

      _editPage(evt){
        console.log("Change Page to New Baseline Policy");
        console.log(evt.detail);
      }
    }
    customElements.define(BaselinePolicyDomainAjax.is, BaselinePolicyDomainAjax);
  </script>
</dom-module>

出于保密考虑,我删除了 url,但我已经测试了 iron-ajax 组件,并且能够成功调用 API。

日志没有告诉我为什么听众没有从其他聚合物元素“听到”我的事件。关于我做错了什么有什么想法吗?

【问题讨论】:

  • 事件在层次结构中向上传播,因此您无法在同级中捕获它们,这与 Polymer 无关,这就是事件的一般工作方式..这里的问题有所不同,但是答案非常相似:stackoverflow.com/a/50292188/521598
  • @mishu 这很有道理,你有什么文档可以帮助我教我如何将数据从父母传递给孩子吗?就像您在回复中所说的那样,它需要数据绑定。我是 Web 开发的新手,希望得到一些指导。

标签: javascript polymer polymer-2.x


【解决方案1】:

正如 Mishu 在评论中所说,事件在层次结构中向上传播。因此,在您的baseline-policies-tab 中,您需要捕获该事件并将其手动传递给baseline-policy-domain-ajax。

一些例子:

<dom-module id="baseline-policies-tab">
<template>
<style include="dfw-styles">
  :host {
    display: block;
  }
</style>
<baseline-policy-create on-domain-ajax="_onDomainAjax"></baseline-policy-create>
<baseline-policy-domain-ajax></baseline-policy-domain-ajax>
</template>

<script>

class BaselinePoliciesTab extends Polymer.Element {
  static get is() {
    return 'baseline-policies-tab';
  }

  static get properties() {
  }

  _onDomainAjax(e) {
    this.shadowRoot.querySelector("baseline-policy-domain-ajax")._editPage(e);
  }
}
customElements.define(BaselinePoliciesTab.is, BaselinePoliciesTab);

</script>
</dom-module>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多