在常规JS文件中使用Meteor Iron Router数据上下文
Use Meteor Iron Router data context in regular JS file?
铁路由器代码:
this.route('petProfile', {
path: '/pets/:_id',
data: function(){
return Adoptees.findOne(this.params._id);
}
});
这很简单。如果我对Adoptees
集合中的每个对象都有一个petName
键,我就可以使用handlebars模板来访问数据上下文。
petProfile.html:
<template name="petProfile">
<h1>{{petName}}</h1>
</template>
但是假设我想在关联的javascript文件中拥有数据上下文:
petProfile.js:
Template.petProfile.rendered = function() {
var petName = //How do I get the pet name from the data context in Iron Router?
}
很好,它有效。所以根据saimunt的答案,我可以这样做:
petProfile.js:
Template.petProfile.rendered = function() {
var petName = this.data.petName;
}
在created / rendered / destroyed
回调中,您可以使用this.data.field
访问数据上下文,因为this
映射到模板实例。
在帮助程序和事件处理程序中,您可以使用this.field
访问它,因为this
直接映射到数据上下文。
可以使用'this'在模板中访问数据
this.petName
相关文章:
- 如何使用Iron Router在Meteor中生成XML文件
- Meteor iron:路由器this.render()正在BeforeAction钩子中阻塞
- Meteor Iron路由器通过[data]vs[params]访问变量
- Meteor,Iron Router,&谷歌分析嵌入API
- Meteor/Iron路由器-何时何地运行JS以交换到所见即所得
- 如何在Meteor Iron路由器的内部模板中访问全局数据
- Iron Router,Meteor 应用程序崩溃时 router.js 被放置在 /lib 中
- Iron Router 和 Meteor 中的服务器端路由
- 使用 Meteor 和 Iron Router 渲染 PDF
- Iron router on Action 使用 Meteor.call 抛出 writeHead 不是一个函数
- Meteor,Iron:路由器在Router.go上传递多个属性
- Meteor Iron路由器如何获取POST数据
- Meteor iron路由器中的next()对此完全困惑
- Meteor中的Iron路由器包问题
- 使用Meteor with Iron:Router,js打开一条保证刷新的路由
- Meteor/Iron路由器-在{{#each}}循环中获取父数据
- iron路由器在waitOn功能-Meteor中选择不同的订阅
- 在Meteor中的Iron Router中使用模板辅助对象上的params
- 如何在Meteor Iron路由器中的一条路由中不显示默认模板
- 在Windows7/Windows8上安装Iron Meteor时出错