目前我正在与Meteor.js建立社交网络,并面临路由问题。这是所需的路由器行为:
这就是我的解决方案:
Router.map ->
@route 'base',
path: '/'
waitOn: ->
waitOnUser()
action: ->
say 'base route'
user = Meteor.user()
if user?
say 'redirecting to username from base route'
username = user.profile.username
@redirect '/user/' + username
else
@redirect 'hello'
@route 'profile', {
path: 'user/:username'
layoutTemplate: 'mainLayout'
template: 'profileLayout'
waitOn: ->
[
waitOnUser(),
Meteor.subscribe('pageOwner', @params.username)
]
onBeforeAction: ->
Session.set('profileOwner', Meteor.users.findOne({'profile.username': @params.username}))
@next()
data: ->
if @ready()
user = Meteor.users.findOne({'profile.username': @params.username})
profile = user.profile
id = user._id
owner = Meteor.user().profile.username is profile.username
{
profile: profile
userId: id
owner: owner
}
action: ->
@render()
waitOnUser = ->
user = Meteor.user()
handle =
ready: ->
if user?
true
else
false
问题在于每次使用' /:username'被调用并重定向到登录用户主页,因此无法观看其他用户页面。
你能为我的案子建议一些更好的解决方案吗?
答案 0 :(得分:0)
找到解决方案,只需更改路径定义的顺序,以便在'base'之前调用'profile'路径