Meteor:使用Iron Router的用户配置文件页面

时间:2014-09-28 11:29:14

标签: javascript meteor iron-router

我正在努力使用铁路由器创建用户个人资料页面,该路由器位于localhost:3000/:username。个人资料页面应具有以下特征:

  • 公开视图 - 任何人都可以看到有关用户的基本信息
  • 私密视图 - 如果客户在登录时访问自己的个人资料页面,则会显示其敏感用户数据并且具有编辑功能
  • 加载视图 - 在提取用户个人资料数据时,显示加载屏幕
  • 未找到视图 - 如果在URL中输入了无效的用户名,则返回未找到的页面。

公共视图和私有视图应存在于相同的 URL路径中。根据客户端的凭据,他们会看到一个或另一个没有重定向到其他页面。未找到的页面也不应该重定向,这样如果输入无效的用户名,用户仍然可以在浏览器URL栏中看到无效的URL。

我的router.js文件:

this.route('profile', {
    controller: 'ProfileController',
    path: '/:username'
  });

ProfileController内,我试图拼凑以下内容:

  • onBeforeAction - 显示加载屏幕;确定用户名是否存在(即URL是否有效)
    • 显示未找到的视图,私人个人资料或公开个人资料
  • waitOn - 在删除加载屏幕之前等待username的数据被检索
  • onAfterAction - 删除加载屏幕

谢谢!

1 个答案:

答案 0 :(得分:12)

幸运的是,您正在寻找的每个特征都可以在插件中找到,因此您甚至不必潜入定义自己的钩子。

请注意,我正在使用iron:router@1.0.0-pre2,这对于跟上最新的内容非常重要,目前只有两个小怪癖,我希望很快就能解决。

让我们从用户个人资料发布开始,它以用户名作为参数。

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/目录),客户端和服务器都可以使用。 / p>

现在最棘手的部分是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检测到您在waitOn中使用RouteController时,它现在会自动添加默认的loading挂钩,该挂钩负责呈现loadingTemplate订阅尚未准备就绪。

我现在要解决我在回答问题时谈到的两个小错误。

首先,官方iron:router指南(您绝对应该阅读)http://eventedmind.github.io/iron-router/提到您应该传递给dataNotFound插件的选项名称为dataNotFoundTemplate但是自2014年9月28日起,这不起作用,您需要使用遗留名称notFoundTemplate,这可能会在几天内得到解决。

控制器中data函数的代码也是如此:我通常使用反直觉语法Router.current().params来访问路径参数this.params适当的常规语法。这是另一个尚未解决的问题。 https://github.com/EventedMind/iron-router/issues/857