幸运的是,您所寻找的每一个特征都可以在插件中烘焙,因此您甚至不必在定义自己的钩子时进行深入研究。
请注意,我正在使用iron:[email protected]
,这对于跟上最新内容很重要,目前只有两个小问题,我希望能很快得到解决。
让我们从以用户名作为参数的用户配置文件发布开始。
server/collections/users.js
Meteor.publish("userProfile",function(username){
// simulate network latency by sleeping 2s
Meteor._sleepForMs(2000);
// try to find the user by username
var user=Meteor.users.findOne({
username:username
});
// if we can't find it, mark the subscription as ready and quit
if(!user){
this.ready();
return;
}
// if the user we want to display the profile is the currently logged in user...
if(this.userId==user._id){
// then we return the corresponding full document via a cursor
return Meteor.users.find(this.userId);
}
else{
// if we are viewing only the public part, strip the "profile"
// property from the fetched document, you might want to
// set only a nested property of the profile as private
// instead of the whole property
return Meteor.users.find(user._id,{
fields:{
"profile":0
}
});
}
});
让我们继续配置文件模板,没有什么太花哨这里,我们会显示用户名作为公共数据,如果我们正在查看的专用配置文件,显示该用户的真实姓名,我们假设存储在profile.name
。
client/views/profile/profile.html
<template name="profile">
Username: {{username}}<br>
{{! with acts as an if : the following part won't be displayed
if the user document has no profile property}}
{{#with profile}}
Profile name : {{name}}
{{/with}}
</template>
然后,我们需要定义在全球路由器配置纵断面图的路线:
lib/router.js
// define the (usually global) loading template
Router.configure({
loadingTemplate:"loading"
});
// add the dataNotFound plugin, which is responsible for
// rendering the dataNotFound template if your RouteController
// data function returns a falsy value
Router.plugin("dataNotFound",{
notFoundTemplate: "dataNotFound"
});
Router.route("/profile/:username",{
name:"profile",
controller:"ProfileController"
});
注意iron:router
现在需要你定义路径和路由控制器在共享目录中(通常这是lib/
dir at t他是您的项目的根源)可用于客户端和服务器。
现在到了最棘手的部分,该ProfileController
定义:
lib/controllers/profile.js
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
});
}
});
当iron:router
检测到您在使用RouteController
它waitOn
现在会自动添加默认loading
挂钩负责用于在订阅尚未准备好时呈现loadingTemplate
。
我现在要谈的是我在回答开始时提到的两个小错误。
首先,官方iron:router
指南(你一定要读)http://eventedmind.github.io/iron-router/提到,你应该传递给dataNotFound
插件选项的名称为dataNotFoundTemplate
但截至28-09-2014这是不行的,你需要要使用传统名称notFoundTemplate
,这可能会在几天内得到解决。
这同样适用于我的data
功能的控制器代码:我已经使用了反直觉的语法Router.current().params
访问路由参数时,通常this.params
会一直恰当的规则语法。这是另一个尚未解决的问题。 https://github.com/EventedMind/iron-router/issues/857
非常感谢您的帮助! Meteor.publish()函数仅用于发布有关用户的选择数据。 – 2014-09-29 03:56:09
这是一篇很棒的文章,谢谢。值得注意的是,未来的读者将无法使用安装了[audit-argument-checks](https://atmospherejs.com/meteor/audit-argument-checks)软件包的代码。 – AlecRust 2015-10-27 20:24:48
建议编辑来解决这个问题。 – AlecRust 2015-10-27 20:40:04