- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在努力创建一个用户配置文件页面,使用位于 localhost:3000/:username
的 Iron Router。个人资料页面应具有以下特征:
公共(public) View 和私有(private) View 应存在于相同 URL 路径中。根据客户的凭据,他们会看到一个或另一个,而无需重定向到不同的页面。未找到的页面也不应该重定向,这样如果输入无效的用户名,用户仍然可以在浏览器 URL 栏中看到无效的 URL。
我的 router.js 文件:
this.route('profile', {
controller: 'ProfileController',
path: '/:username'
});
在 ProfileController
中,我试图将以下内容拼凑在一起:
onBeforeAction
- 显示加载屏幕;确定用户名是否存在(也就是 URL 是否有效)
waitOn
- 在移除加载屏幕之前等待用户名
的数据被检索onAfterAction
- 移除加载屏幕谢谢!
最佳答案
幸运的是,您正在寻找的每个特性都可以在插件中烘焙,因此您甚至不必深入定义自己的钩子(Hook)。
请注意,我正在使用 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
}
});
}
});
让我们继续配置文件模板,这里没什么特别的,我们会将用户名显示为公共(public)数据,如果我们正在查看私有(private)配置文件,则显示我们假设存储在 profile 中的用户真实姓名。姓名
。
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>
然后我们需要在全局路由器配置中为配置文件 View 定义一个路由:
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
现在要求您在共享目录中定义路由和路由 Controller (通常这是项目根目录中的 lib/
目录) 对客户端和服务器都可用。
现在是最棘手的部分,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
钩子(Hook)负责在订阅尚未准备好时呈现 loadingTemplate
。
我现在将解决我在回答开始时提到的两个小错误。
首先,官方iron:router
指南(您绝对应该阅读)http://eventedmind.github.io/iron-router/提到您应该传递给 dataNotFound
插件的选项名称是 dataNotFoundTemplate
但截至 28-09-2014 这将不起作用,您需要使用旧版name notFoundTemplate
,这可能会在几天内得到修复。
Controller 中我的 data
函数的代码也是如此:我使用了违反直觉的语法 Router.current().params
来访问通常 this.params
是适当的常规语法时的路由参数。这是另一个尚未解决的问题。 https://github.com/EventedMind/iron-router/issues/857
关于javascript - Meteor:带有 Iron Router 的用户配置文件页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26084419/
我有一个已发布的库,其中包含一个在其模板中使用 [routerLink] 的组件。在我的应用程序中安装库后,我收到错误 NullInjectorError:R3InjectorError(AppMod
/~/@ReMix-Run/Router/dist/router.cjs.js模块分析失败:/HOME/sharib/Desktop/Full Stack Developer Tutorial/MER
在 vuejs 2 中,我能够通过返回渲染 html 标签直接在路由器中渲染。我正在尝试使用 vue-router 4 在 vue3 中做同样的事情,但它似乎不起作用: { path:
在我的应用程序中,我使用 vue-router。当我使用 router.map({}) 将路由传递给路由器时,路由可以工作,但当我在构造函数中传递它们时,路由就不起作用。知道这是为什么吗? // wo
我正在 react 应用程序中实现路由。我对react-router 4.0相对于react-router-redux的优势感到困惑。我也对react-router-dom感到困惑。 react-ro
我有以下两条路线:/items和/items/buy。 每个路线在一个 View 中对应一个选项卡。两条路线均使用精确的 Prop 进行渲染,但导航至/items/buy时,仍将两个选项卡标记为事件选
我有一个带有 url/product/0 的新产品页面。当使用按钮保存产品时,我想显示“已成功保存”消息并将 url 更改为/product/1024 而无需实际导航到该页面,因为这会导致对页面上的产
我对此有些困惑。我定义了一条名为classes/:id的路线。在应用程序中导航到该路线时,将调用componentDidMount()。但是,当重新加载页面或复制并粘贴URL时,页面将完全加载,但根本
假设我有以下2条路线: ... ... 现在,当我尝试按路线/news时,会触发带有Home参数的param1的根路线... 我认为解决方案是在像/?param1这
我正在将 Vue Router 与 Vue 3 一起使用,并尝试添加一个包罗万象的路由以在用户尝试访问无效 URL 时重定向用户。当我尝试使用通配符 (*) 时,我将以下错误记录到控制台: Uncau
我在我的项目中使用react-router-dom 4.0.0-beta.6。我有如下代码: 我想在 HomePage 组件中获取查询参数。我找到了 location.search 参数,它看起来像
我正在使用 Vue 路由器。在我的代码中,我曾经有: 这有效,并且页面数据被传递到组件。但是如何使用路由器 View 执行相同的操作呢?这似乎不起作用: js: var vm = new Vue(
我目前正在一个项目中使用react-router-redux,除了我不知道如何访问react-router路由器之外,一切都工作正常。 @connect((state, props) => {
当使用链接时,路由器按预期工作,尽管我收到警告 [history] pushState is deprecated;使用推送代替。 使用react-router-redux中的routeActio
我想创建一个多页 Meteor 网络和移动应用程序。因此,我正在寻找路由服务。 来自 research ,我收集了Iron Router就是我要找的。但是很多教程也提到了Meteor.Router I
我正在使用 React Router(版本 4.0.0)和 Router组件有一个名为“history”的必需 Prop 。 我知道在以前版本的react-router中你可以导入browserHis
我正在使用 氟罗包用于导航。 Fluro 和 Flutter 正在使用“路由器”。所以他们的类(class)合并了。我怎么能解决这个问题? lib/routers/routers.dart:2:1:
自从我更新了 Vue.js 应用程序的节点包后,我在浏览器控制台中收到以下警告: [vue-router] 's tag prop is deprecated and has beenremoved
我正在尝试在我的应用程序中将 react-router 更新到 v2.6 并将 react-router-relay 更新到 v0.7,但我正在努力按照变更日志来解决所有重大变更。我想我解决了所有的变
router-view 和 router-link 组件无法在我的应用中解析不知道是什么问题 附上应用代码它是一个主页应用程序,带有类似 navbar 的东西,用于路由到 vue 组件,如 state
我是一名优秀的程序员,十分优秀!