2015-07-21 36 views
4

我剛開始使用聚合物。似乎有兩個事件表明內容已準備就緒:哪一個先發生? WebComponentsReady或dom-change?

// Listen for template bound event to know when bindings 
// have resolved and content has been stamped to the page 
app.addEventListener('dom-change', function() { 
    console.log('Our app is ready to rock!'); 
}); 

// See https://github.com/Polymer/polymer/issues/1381 
window.addEventListener('WebComponentsReady', function() { 
    // imports are loaded and elements have been registered 
}); 

我不知道是否有必要一起包起來,把裏面的代碼,以確保文件被完全加載做任何腳本之前,例如:

app.addEventListener('dom-change', function() { 
    window.addEventListener('WebComponentsReady', function() { 
    // scripts go here 
    }); 
}); 

但是,我不知道在所有瀏覽器中這樣做的正確方法是什麼。如果WebComponentsReady發生在dom-change之前,那麼內部腳本永遠不會執行。

哎呀,這可能不是必須的,因爲聚合物起始劑套件不會將它們包裹在一起。在這種情況下,dom-change事件中哪些類型的腳本應該進入,哪些類型的腳本應該進入WebComponentsReady事件?

+0

可能相關的問題:http://stackoverflow.com/questions/21763690/polymer-and-webcomponentsready-event – aug

回答

1

使用原生ready回調爲described here

<script> 
    (function() { 
    Polymer({ 
     is: 'example-element', 
     properties: {...}, 
     ready: function() { 
     // access a local DOM element by ID using this.$ 
     this.$.header.textContent = 'Hello!'; 
     } 
    }); 
    })(); 
</script> 
相關問題