Polymer 1.0自定义火灾事件没有捕获

时间:2015-07-04 10:04:39

标签: polymer-1.0

我无法捕捉从my-login元素触发的自定义事件,并带有一个按钮到my-overview元素。我真的不知道为什么这不起作用。

的index.html

<iron-pages attr-for-selected="data-route" selected="{{route}}">
  <section data-route="login">
    <paper-material id="pmLogin" elevation="1">
      <paper-toolbar>
        <h1 class="paper-font-display1"><span>Login</span></h1>
      </paper-toolbar>
      <my-login id="elLogin"></my-login>
    </paper-material>
  </section>

  <section data-route="overview">
    <paper-material id="pmOverview" elevation="1">
      <paper-toolbar>
        <h1 class="paper-font-display1"><span>Overview</span></h1>
      </paper-toolbar>
      <my-overview id="elOverview"
                   on-call-overview-refresh="reloadOverview">
      </my-overview>
    </paper-material>
  </section>
</iron-pages>

我-的login.html

routeTo: function(route) {
  var app = document.querySelector('#app');
  app.route = route;
  this.fire('call-overview-refresh');
},

我-overview.html

reloadOverview: function() {
  ...
}

1 个答案:

答案 0 :(得分:2)

简而言之,您需要在实际触发自定义事件的元素上声明on-*事件处理程序 - 在本例中为<my-login>

看到您的<iron-pages>位于 index.html ,我认为标记已包含在<template is="dom-bind"></template>内?如果是这种情况,您的index.html可能如下所示:

<template id="app" is="dom-bind">
  ...
  <iron-pages attr-for-selected="data-route" selected="{{route}}">
    <section data-route="login">
      <paper-material id="pmLogin" elevation="1">
        <paper-toolbar>
          <h1 class="paper-font-display1"><span>Login</span></h1>
        </paper-toolbar>
        <my-login id="elLogin"
                  on-call-overview-refresh="callReloadOverview"></my-login>
      </paper-material>
    </section>

    <section data-route="overview">
      <paper-material id="pmOverview" elevation="1">
        <paper-toolbar>
          <h1 class="paper-font-display1"><span>Overview</span></h1>
        </paper-toolbar>
        <my-overview id="elOverview"></my-overview>
      </paper-material>
    </section>
  </iron-pages>
  ...
</template>

<script>
  window.addEventListener("WebComponentsReady", function (e) {
    var app = document.querySelector("#app");
    app.callReloadOverview = function () {
      app.$.elOverview.reloadOverview();
    }
    ...
  });
</script>

在上面的代码段中,当<my-login>触发call-overview-refresh事件时,系统会调用callReloadOverview()函数,然后调用<my-overview>的{​​{1}} reloadOverview()方法