将数据从 Iron Router DATA 函数传输到模板
Transfering data from iron router DATA function to template
所以我掌握了这个 DATA 上下文,但不知道如何将它传递给模板。任何人都可以向我展示或指导我举个例子吗?我是否需要一个模板助手,我将再次进入数据库以检索数据,因为我已经在 iron router 中拥有数据上下文...?
ROUTER.js
Router.route("/employer", {
name:"employer",
template:"employer",
layoutTemplate:'employerLayout',
// controller:"ProfileController",
data: function(){
var currentuser = Meteor.userId();
Meteor.call("getEmployer", currentuser, function(error, result){
if(error){
console.log("error", error);
}
if(result){
return result;
}
});
},
EMPLYER.html
<template name="employer">
<h1> Employer</h1>
<h2>{{result}}</h2>
</template>
COLLECTION
meteor:PRIMARY> db.employer.find().pretty()
{
"_id" : "qCFGZa4ogc5LR56PL",
"createdAt" : ISODate("2015-07-18T13:19:16.098Z"),
"user" : "owfJ4ozrfsp26o8G4"
}
比如我想写出来
<template name="employer">
<h1> Employer</h1>
<h2>{{_id}}</h2>
<h2>{{createdAt}}</h2>
<h2>{{user}}</h2>
</template>
data
挂钩需要同步,因此您不能从其中调用方法(并期望结果)。但是,您可以通过 data
挂钩中对 this question. Alternatively, you could try calling a reactive-method 的任何答案轻松地将此责任转移到您的模板 - 我从未真正这样做过,但它似乎可以工作:
data: function() {
var currentuser = Meteor.userId();
return ReactiveMethod.call('getEmployer', currentUser);
}
旁注 - 如果事实证明你总是只为当前用户调用 getEmployer
,那么你不需要传递 Meteor.userId()
,因为 this.userId 可用于方法。
所以我掌握了这个 DATA 上下文,但不知道如何将它传递给模板。任何人都可以向我展示或指导我举个例子吗?我是否需要一个模板助手,我将再次进入数据库以检索数据,因为我已经在 iron router 中拥有数据上下文...?
ROUTER.js
Router.route("/employer", {
name:"employer",
template:"employer",
layoutTemplate:'employerLayout',
// controller:"ProfileController",
data: function(){
var currentuser = Meteor.userId();
Meteor.call("getEmployer", currentuser, function(error, result){
if(error){
console.log("error", error);
}
if(result){
return result;
}
});
},
EMPLYER.html
<template name="employer">
<h1> Employer</h1>
<h2>{{result}}</h2>
</template>
COLLECTION
meteor:PRIMARY> db.employer.find().pretty()
{
"_id" : "qCFGZa4ogc5LR56PL",
"createdAt" : ISODate("2015-07-18T13:19:16.098Z"),
"user" : "owfJ4ozrfsp26o8G4"
}
比如我想写出来
<template name="employer">
<h1> Employer</h1>
<h2>{{_id}}</h2>
<h2>{{createdAt}}</h2>
<h2>{{user}}</h2>
</template>
data
挂钩需要同步,因此您不能从其中调用方法(并期望结果)。但是,您可以通过 data
挂钩中对 this question. Alternatively, you could try calling a reactive-method 的任何答案轻松地将此责任转移到您的模板 - 我从未真正这样做过,但它似乎可以工作:
data: function() {
var currentuser = Meteor.userId();
return ReactiveMethod.call('getEmployer', currentUser);
}
旁注 - 如果事实证明你总是只为当前用户调用 getEmployer
,那么你不需要传递 Meteor.userId()
,因为 this.userId 可用于方法。