如何在没有事件的情况下讨论自定义Polymer元素

时间:2015-01-26 11:00:24

标签: javascript angularjs polymer

我想在父自定义元素中没有事件的自定义元素之间进行讨论。 如果可能的话,我怎么能这样做?

这是 my-custom-parent-element.htlm

<link rel="import" href="../my-custom-elment-1.html">
<link rel="import" href="../my-custom-elment-2.html">
<polymer-element name="my-custom-parent-element" noscript>
    <template>
        <my-custom-elment-1></my-custom-elment-1>
        <my-custom-elment-2></my-custom-elment-2>
    </template>
</polymer-element>

这是 my-custom-element-1

<link rel="import" href="../polymer-gestures/polymer-gestures.html">
<polymer-element name="my-custom-element-1">
    <template>
        <div><canvas width="300px" height="200px"></canvas></div>
    </template>
    <script>
        Polymer({
            ready: function() {
                <!-- Here, I need notify my-custom-element-2 that canvas received an event without re-send an event-->
            }
        });
    </script>
</polymer-element>

这是 my-custom-element-2

<polymer-element name="my-custom-element-2">
    <template>
        <div></div>
    </template>
    <script>
        Polymer({
            ready: function() {
                <!-- Here, I need to change value by my-custom-element-1 notifications -->
            }
        });
    </script>
</polymer-element>

1 个答案:

答案 0 :(得分:0)

不确定要怎么做。 但是你也可以从父母那里引用elem1:

&#13;
&#13;
    <polymer-element name="my-custom-parent-element" noscript>
    <template>
        <my-custom-elment-1 id="elem1"></my-custom-elment-1>
        <my-custom-elment-2 id="elem2"></my-custom-elment-2>
    </template>
    <script>
Polymer({
            domReady: function() {
                this.$.elem1.target = this.$.elem2
            }
        });
    </script>
</polymer-element>
&#13;
&#13;
&#13;

在elem1:

&#13;
&#13;
<polymer-element name="my-custom-element-1" attributes="target">
    <template>
        <div><canvas width="300px" height="200px"></canvas></div>
    </template>
    <script>
        Polymer({
            ready: function() {
                <!-- Here, I need notify my-custom-element-2 that canvas received an event without re-send an event-->
                  target.xxx();
            }
        });
    </script>
</polymer-element>
&#13;
&#13;
&#13;

但通常,事件方式更好