2014-03-26 34 views
1

什麼是在流星中使用Template.myTemplate.rendered ?.我曾研究過這個文檔,但是我不明白確切的功能。該文檔說Provide a callback when an instance of a template is rendered。什麼是模板的實例呈現的含義。請給我一個例子的建議嗎?Template:myTemplate.rendered in Meteor?

回答

2

當回調被觸發時,模板的一個實例已被渲染(添加到文檔中)。例如,如果您想通過使用JavaScript庫(例如CodeMirror)來操作模板中的元素,就像我在其中一個項目中使用的那樣,這會很有用。例如:

<template name="test"> 
    <textarea></textarea> 
</template> 
Template.test.rendered = function(){ 
    // Make the textarea highlight the code (kind of). 
    var myCodeMirror = CodeMirror.fromTextArea(this.find('textarea')) 
} 
+0

能否請你解釋一下渲染功能時執行@佩普LG – Venkat

+0

@Venkat它的HTML模板被插入到DOM(因此後立即執行,它可以安全地與操縱?像jQuery)。 –

+0

我嘗試執行上述相同scenario.I測試與console.log,但從來沒有去渲染方法。所以什麼是使用上述呈現的方法。@布拉德M – Venkat