【问题标题】:Templete renders else block, on reload, renders if模板呈现 else 块,在重新加载时呈现 if
【发布时间】:2023-03-15 08:27:01
【问题描述】:

我有以下模板,当用户单击特定帖子上的“谁发布这个”按钮时加载该模板。

<template name="profile">

{{#if existinguser}}
`//other code to render when esitinguser returns true//`
    {{else}}
    <p>This user doesn't exist.</p>
    {{/if}}
</template>

这是现有的用户助手:

'existinguser': function() {


        if (Meteor.users.find({
                "profile.username": document.URL.split('/')[4]
            }).count() > 0) {
            return true;
        } else {
            return false;
        }


    }

还有 route.js

Router.route('/user/:username', {
    name: 'profile',
    loadingTemplate: 'loading',
    waitOn:function(){Meteor.subscribe('users');
},
    data: function() {
        return Meteor.users.find({
            'profile.username': this.params.username
        });



    }
});

现在,当点击链接时,else 块被渲染并且页面显示这个用户不存在,即使它在用户集合中。

如果我在同一个 url 上重新加载页面,它会呈现并显示 if 块中的内容。

可能是什么问题?

【问题讨论】:

  • 你可以使用Router.current().params.username而不是document.URL.split('/')[4],那么你正在使用的有点hacky
  • 路由器功能会在流星帮助 js 文件中工作吗?
  • 你为什么不试试呢?
  • 你试过我的回答并在 Meteor.subscribe 调用前面加上“return”吗?

标签: javascript html templates meteor


【解决方案1】:

你应该从订阅返回状态值

请尝试:

waitOn:function(){ return Meteor.subscribe('users'); },

另见文档:

https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#the-waiton-option

【讨论】:

    猜你喜欢
    • 2014-03-28
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多