2014-03-13 29 views
0

我用烙鐵元素重定向我的用戶有我的主要元素:WebComponentsReady不重新觸發

.... 
<template if="{{route != null}}"> 
    <template if="{{route == 'home' || route == ''}}"> 
    <home-element structure="{{home}}"></home-element> 
    </template> 
    <template if="{{route == 'research'}}"> 
    <research-element structure="{{research}}"></research-element> 
    </template> 
    <template if="{{route == 'highlights'}}"> 
    <!-- <highlights-element></highlights-element> --> 
    </template> 
</template> 
.... 

.... 
Polymer('research-element', { 
     name: 'research', 
     ready: function(){ 
     // wait for the WebComponent to be ready to hook up jquery 
     var self = this; 
     addEventListener('polymer-ready', function() { 
      window.console.log($('#'+self.name+'-affix')); 
      $('#'+self.name+'-affix').affix(); 
     }); 
.... 

1在登錄(#home),用戶獲得定向到「家元」 ,並且WebComponentsReady被正確觸發。

2-如果我想要轉到另一個頁面(#research),用戶將被引導到'research-element'。值得注意的是,一旦「研究元素」準備就緒,WebComponentsReady就不會重新啓動。

預計還是我做錯了什麼?

另外,還有什麼好的方法來緩存元素,一旦他們被加載?

最佳, 薩科

回答

2

polymer-ready事件是向外界傳遞的信號,表明進口已加載,自定義元素已升級,Polymer已完成設置。你不應該在你的元素裏面聽。它只適用於主應用程序(例如index.html)。

相反,使用attached/detached lifecycle callbacks知道當每個元素被添加/刪除從DOM:

<script> 
    Polymer('home-element', { 
    attached: function() { 
     console.log('home-element attached'); 
    }, 
    detached: function() { 
     console.log('home-element detached'); 
    } 
    }); 
</script> 

演示:http://jsbin.com/zeyoyisu/1/edit

+0

太棒了正是我所期待的 – Nicolas

0

如果你使用的聚合物,而不是聽出來的WebComponentsReady事件,嘗試polymer-ready。這是我們介紹的最近事件,當您的元素已加載並準備就緒時會觸發該事件。快速demo

對於您的特定元素,您是否可以嘗試上述並讓我們知道您是否仍遇到問題?您也可以嘗試從research-element以內的this.fire()開始自定義事件,只要您知道它已準備就緒。

+0

所以我想,我意識到我可能做錯了什麼:我我正在傾聽「聚合物就緒」事件,在我的研究元素準備方法中。然後當它準備好時,我想要做的東西。 (看我原來的帖子上的編輯) – Nicolas

+0

所以是的,沒有運氣,還沒有工作。這是預期的行爲嗎? – Nicolas

+0

這兩個事件都沒有觸發,看起來如果在內部有任何錯誤,事件不會觸發,但錯誤不會在控制檯上報告(例如未命中導入)。我將所有WebComponentsReady移動到my-app.read() – wener