使用 iron-router 访问 onRender 模板的用户配置文件信息
Access user profile information for onRender template using iron-router
我正在尝试在客户端代码中为我的用户个人资料页面访问用户数据 onRendered。
我的客户端有这个:
Template.profile.onRendered(function () {
$('#qrblock').qrcode({text: <I want to show user's qrUrl here>});
});
这是创建用户时的用户架构:
Accounts.createUser({
username: username,
emails: [
{ address: email, verified: false }
],
password: password,
createdAt: new Date(),
profile:{
firstName: firstName,
lastName: lastName,
},
qrUrl: 'test.meteor.com/@' + username
},
...
...
...
这是我的模板:
<template name="profile">
<div class="container-fluid">
Username: {{username}}<br />
{{#with profile}}
Profile name: {{firstName}} {{lastName}}
{{/with}}
<div id="qrblock"></div>
</div>
</template>
这是我的路由器和控制器:
Router.route('/@:username', {
name: 'profile',
controller: 'ProfileController'
});
ProfileController = RouteController.extend({
template: 'profile',
waitOn: function() {
return Meteor.subscribe('userProfile', this.params.username);
},
data: function() {
var username = Router.current().params.username;
return Meteor.users.findOne({
username: username
});
}
})
我对 meteor routing 的东西不太了解,所以在访问特定个人资料页面的用户数据时让我感到困惑。我希望我正在尝试做的事情是可能的。
如何在我的配置文件模板 onRendered 函数中使用用户数据?
您可以使用 this
.
在 Template.myTemplate.onCreated
, Template.myTemplate.onRendered
and Template.myTemplate.onDestroyed
函数中访问模板实例
如果要访问数据上下文,可以使用this.data
:
Template.profile.onRendered(function() {
$('#qrblock').qrcode({
text: this.data.qrUrl
});
});
对于将来遇到类似问题的任何人:
通过简单地检查页面的 url 并将其设为二维码文本,有一种更简单、更动态的方法可以实现我在这里想要的效果。这消除了为每个用户存储 qrUrl 的需要,这意味着您不必为测试站点和已部署站点使用不同的版本。
代码在这里:
Template.profile.onRendered(function () {
console.log(this.data.qrUrl);
var url = window.location.href;
$('#qrblock').qrcode({text: url});
});
我正在尝试在客户端代码中为我的用户个人资料页面访问用户数据 onRendered。
我的客户端有这个:
Template.profile.onRendered(function () {
$('#qrblock').qrcode({text: <I want to show user's qrUrl here>});
});
这是创建用户时的用户架构:
Accounts.createUser({
username: username,
emails: [
{ address: email, verified: false }
],
password: password,
createdAt: new Date(),
profile:{
firstName: firstName,
lastName: lastName,
},
qrUrl: 'test.meteor.com/@' + username
},
...
...
...
这是我的模板:
<template name="profile">
<div class="container-fluid">
Username: {{username}}<br />
{{#with profile}}
Profile name: {{firstName}} {{lastName}}
{{/with}}
<div id="qrblock"></div>
</div>
</template>
这是我的路由器和控制器:
Router.route('/@:username', {
name: 'profile',
controller: 'ProfileController'
});
ProfileController = RouteController.extend({
template: 'profile',
waitOn: function() {
return Meteor.subscribe('userProfile', this.params.username);
},
data: function() {
var username = Router.current().params.username;
return Meteor.users.findOne({
username: username
});
}
})
我对 meteor routing 的东西不太了解,所以在访问特定个人资料页面的用户数据时让我感到困惑。我希望我正在尝试做的事情是可能的。
如何在我的配置文件模板 onRendered 函数中使用用户数据?
您可以使用 this
.
Template.myTemplate.onCreated
, Template.myTemplate.onRendered
and Template.myTemplate.onDestroyed
函数中访问模板实例
如果要访问数据上下文,可以使用this.data
:
Template.profile.onRendered(function() {
$('#qrblock').qrcode({
text: this.data.qrUrl
});
});
对于将来遇到类似问题的任何人:
通过简单地检查页面的 url 并将其设为二维码文本,有一种更简单、更动态的方法可以实现我在这里想要的效果。这消除了为每个用户存储 qrUrl 的需要,这意味着您不必为测试站点和已部署站点使用不同的版本。
代码在这里:
Template.profile.onRendered(function () {
console.log(this.data.qrUrl);
var url = window.location.href;
$('#qrblock').qrcode({text: url});
});