簡體   English   中英

嘗試在另一個Polymer元素中附加一個Polymer元素

[英]Trying to append a Polymer element in another Polymer element

我有一個v-fire聚合物:

<script>
    Polymer({
        is: 'v-fire',

        properties : {
            onFire: {
                type : String,
                reflectToAttribute: true
            }
        },

        firing: function () {
            this.fire('fire');
        }
    })
</script>

我希望能夠在Polymer元素中的任何位置使用它,以使其觸發內部函數,以使其執行諸如update之類的特定任務,以便在v-fire調用firing時將它們全部更新。

例如,我創建了一個新對象進行測試:

<script>
    Polymer({
        is: 'fire-tester',

        _updateContent: function () {
            alert('I get updated');
        }
    });
</script>

index.html中

…
<link rel="import" href="/components/fire-tester/fire-tester.html">
…
<body>
<fire-tester id="fire-tester"></fire-tester>
<script>
(function () {

    var ft = document.getElementById('fire-tester');

    // make a global v-fire Polymer
    var vfire = document.createElement('v-fire');
    // custom callback of the Polymer's that will include the v-fire
    vfire.onFire = '_updateContent';

    /**
     * And here, I try to insert the v-fire in the fire-tester Polymer
     */
    Polymer.dom(ft.root).insertBefore(
        vfire,
        Polymer.dom(ft.root).childNodes[0]
    );

    // the dom is pretty neat, fire-tester contains v-fire with the custom on-fire callback

    // then I try to fire the event
    vfire.firing(); // but nothing happen


});
</script>

它不起作用,因為我相信將v-fire插入fire-tester中不會得到處理。 有沒有辦法告訴Polymer處理dom塊,就像在本地DOM中聲明它一樣?

看來您正在錯誤地接近事件系統。 您想要的是在檢測到子v-fire元素上的fire事件時在fire-tester上運行一種方法,對嗎? 這就是我將其組合在一起的方式:

V-fire.html

<script>
  Polymer({
    is: 'v-fire',

    firing: function() {
      this.fire('fire');
    }
  });
</script>

消防tester.html

<script>
    Polymer({
      is: 'fire-tester',

      listeners: {
        'fire': '_updateContent'
      },

      _updateContent: function () {
        alert('I get updated');
      }
    });
</script>

的index.html

<fire-tester id="fire-tester"></fire-tester>
<script>
  (function(){
    var ft = document.getElementById('fire-tester');

    var vfire = document.createElement('v-fire');

    Polymer.dom(ft.root).insertBefore(
        vfire,
        Polymer.dom(ft.root).childNodes[0]
    );

    vfire.firing();
  })();
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM