Template instances
一个模板实例对象代表文档对模板的一次引入。模板实例可以用来获取模板中的HTML元素,还可以给模板实例附加属性,属性会在模板响应式更新中保持,不会丢失。
在好几个地方都可以获取到模板实例对象:
- 在
created,rendered和destroyed模板回调中,this指向模板实例 - 事件处理器的第二个参数
- 在Helper中,通过
Template.instance()获取模板实例
你可以选择给模板实例附加属性,来跟踪模板相关的状态。例如,当使用Google Maps API 时,你可以附加map对象到当前模板实例,这样就可以在Helper和事件处理器中引用map对象。用onCreated 和 onDestroyed回调函数来执行初始化或清理工作。
template.findAll(selector)
Client
Find all elements matching selector in this template instance.
Arguments
selector String
The CSS selector to match, scoped to the template contents.
template.findAll返回一个符合selector的DOM元素数组。也可以使用template.$,它的工作方式和JQuery的$函数一样,但是只返回template内部的元素。
template.find(selector)
Client
Find one element matching selector in this template instance.
Arguments
selector String
The CSS selector to match, scoped to the template contents.
find类似findAll但是只返回找到的第一个元素。和findAll一样,find只返回模板内部的元素。