我有聚合物1.0.0定義了兩個Web組件和我的問題是有關訪問父公共API將分量孩子打電話給他父母API高分子1.0.0
<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>
正如你所看到的,我很好奇你如何輕鬆地從孩子們那裏獲得公共方法getAll
?
我見過一些涉及基於事件的解決方案的文檔(傾聽兒童事件),但這並不符合我的需要。除非你告訴我唯一可用的解決方案..
任何想法?
一般來說,我會說雖然不是一個反模式來調用父子方法,但更好的做法是使用事件向上發送信息,並且只能在父子關係中直接調用。 在給出的例子中,你已經使用'dom-repeat'傳遞了'x-photo'的照片,所以不需要向父節點「回調」。綁定'photo =「{{item}}」'將設置'x-photo'的照片屬性。 –
'Polymer.dom(this.root).parentNode.getAll()'應該可以工作,我想。 – Zikes
雖然我必須同意@MichaelBleigh。在兒童元素需要關於其兄弟元素的知識的情況下,也許這種知識應由父母分配?這樣子女的元素就不需要推斷任何關於他們的環境的事情,並且可以專注於做他們被告知的事情。 – Zikes