数据don't登录流星后加载

The data don't load after login meteor

本文关键字:登录 流星 加载 don 数据      更新时间:2023-09-26

我的Meteor应用程序中有一个登录页面。我已经自定义了我的登录名:

{{#if isLogged}}
    {{>index}}
{{else}}
    {{>login}}
{{/if}}

如果用户已登录,我将加载"索引"模板。

所以我成功地登录了一个用户,应用程序在"索引"上路由我,但没有加载集合。

例如,我有:

Template.listWorkspace.rendered = function () {
           //some stuff
    });

当我登录时,这不是老问题。我必须按F5键才能加载我的所有数据。

编辑

这是isLogged声明:

if(Meteor.isClient) {
Template.indexLogin.helpers({
    isLogged : function () {
        var indexIsLoggedUser = Meteor.user();
        return indexIsLoggedUser != null;
    }
});

}

以下是索引模板声明:

 <template name="index">
{{>widgetWorkspace}}
 </template>
<template name="widgetWorkspace">
{{>header}} 
    {{>workspace}}
</template>
 <template name="workspace">
<div class="row">
    <!-- <button class="btn btn-default" id="add"><span class="glyphicon glyphicon-plus"></span>Add widget</button> -->
    <button class="btn btn-success" id="editmode" data-i18n="btn.editMode"></button>
    <button class="btn btn-warning" data-toggle="modal" data-target="#myModal" data-i18n="btn.addWidgets"><span class="glyphicon glyphicon-plus"></span></button>{{>bootstrapModal}}
    <!-- <button class="btn btn-danger" id="addContainer"><span class="glyphicon glyphicon-plus"></span>Add Container</button> -->
    <!--<button class="btn btn-primary" id="serialize">Serialize</button>-->
</div>
<br>
<div id="widgetMainContainer">
    {{#each Widgets}}
        {{>widget}}
    {{/each}}
</div>
 </template>

第2版:

这是用户工作空间的模板:

 <template name="listWorkspace">
 <ul id="workspaces"></ul>
 </template>

这是渲染的一些javascript代码:

Template.listWorkspace.rendered = function () {
    var headerWorkspaceUserLogged = Meteor.users.find({_id: Session.get('user_id')}).fetch();
    var headerWorkspaces = headerWorkspaceUserLogged[0].workspace;
    if(headerWorkspaces == null || headerWorkspaces == undefined) {
        console.log('empty');
    }
    async.forEachSeries(headerWorkspaces, function(item, callback) {
        var d = $('<li/>');
        d.attr('rel', item.id-1).addClass('workspace-mini').click(function() {
            NProgress.start();
            $('#workspaces li').removeClass('workspaceSelected');
            $(this).addClass('workspaceSelected');
            var text = $('<div/>'); 
            text.addClass('workspaceText')
                    .text('workspace ' + (parseInt($(this).attr('rel'))+1))
                    .appendTo($(this))
                    .position({ my: 'center center', at: 'center center', of: $(window)})
                    .fadeIn(function() { $(this).fadeOut(function() { $(this).remove; })});
            current_workspace = headerWorkspaces[parseInt($(this).attr('rel'))];
            current_workspace_number = parseInt($(this).attr('rel'));
            Session.set('current_workspace_number', current_workspace_number+1);
            Session.set('current_workspace', current_workspace);
            //on selectionne le workspace dans la base Mongo en fonction du workspace selectionné
            Meteor.call('updateIdSelectdWorkspace', Session.get('user_id'), Session.get('current_workspace').name);
            NProgress.done();
        });
        $('#workspaces').append(d);
        //Pour l'ajout d'un nouveau workspace on memorise l'id du dernier workspace
        Session.set('i', item.id); 
        callback();
    }, function(err) {
        console.log('done');
    });
    $('#workspaces').append('<div id="addWorkspace"><span class="glyphicon glyphicon-plus"></span></div>');
    $('#addWorkspace').css('float', 'right')
        .css('margin-left', '5px')
        .css('margin-top', '2px')
        .css('cursor', 'pointer');
};

Template.listWorkspace.rendered=函数()在我第一次登录应用程序时不会加载。

有人能告诉我装载流星的顺序吗?:)

这里有很多错误。首先,你不需要自己的助手来检查用户是否登录。根据文档,只需使用Meteor推荐的{{#if currentUser}}即可。

其次,您有一个可供使用的模板系统。为什么不使用它来定义小部件在页面上的显示方式,而不是在rendered函数中以编程方式插入DOM节点呢?为什么要使用async?试着通过模板尽可能少地做所有可能的事情,然后加入帮助程序,只实现模板无法实现的目标。您通常也不需要深入研究Meteor中的回调和异步代码。

试着用流星重置命令重新设置你的项目我在重命名文件后遇到了类似的问题,现在用{{#if currentUser}}助手对我有效。