2014-03-13 30 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