在 Polymer 1.0.0 中制作一个 child 组件调用他的 parent API

Make a child component call his parent API in Polymer 1.0.0

我有两个使用 Polymer 1.0.0 定义的 Web 组件,我的问题是关于访问 parent public API

<dom-module id="x-gallery">
  <template is="dom-repeat" items="{{photos}}">
    <x-photo photo="{{item}}"></x-photo>
  </template>
</dom-module>

<script>
  Polymer({
    is: 'x-gallery',

    ...

    getAll: function() {
      return this.photos;
    }
  });
</script>

<dom-module id="x-photo">
  <template>
    <img src="{{photo.src}}">
  </template>
</dom-module>

<script>
  Polymer({
    is: 'x-photo',
    properties: {
      photo: Object
    },

    ready: function() {
      // HERE ---
      // How could I access the x-gallery.getAll public method?
      // ...
    }
  });
</script>

如您所见,我想知道如何从 children?

轻松访问 getAll public 方法

我看过一些文档提到基于事件的解决方案(监听 child 事件),但这并不真正符合我的需要。除非你告诉我唯一可用的解决方案..

有什么想法吗?

ready: function() {
    this.domHost.getAll()
}

来自文档: http://polymer.github.io/polymer/

"domHost" 是

"element whose local dom within which this element is contained"

通过这种方式您可以访问"parent"及其功能。 在我看来,这不是 Polymer 框架中的正确方法。

我在我的项目中使用它,只是为了定义父级的回调函数。

(抱歉我的英语不好)

已确认..我已经使用了两种方法 - 1. parent 明确设置 child 的 属性 指向回 parent,以及 2.域名主机。 domHost 更简单更好,因为它是 built-in。 在方法一中,你必须确保 child 在设置 属性 之前就绪。