2013-11-22 26 views
0

我在我的流星應用程序中有一個登錄頁面。我已經定製自己的登錄:登錄後才能加載數據流星

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

如果用戶登錄我打開「索引」模板。

因此,我記錄一個成功的用戶和應用程序路線我的「索引」,但沒有集合被加載。

比如我有:

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

當我記錄,這是不LAOD。我必須按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> 

EDIT2:

這是與用戶的工作區中的模板:

<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 =函數()不加載在第一時間當我登錄到應用程序。

有人可以解釋我用流星加載的順序嗎? :)

+0

我們可以看看'isLogged','index'和'login'是如何聲明的嗎?我的直覺是'isLogged'沒有反應,因此改變它不會重新運行模板。 – Joseph

+0

當然,我編輯了我的帖子:) – Ricklemer

+0

您是否也定義了'header'模板? –

回答

0

這裏有很多事情不對。首先,你不需要你自己的幫手來檢查用戶是否登錄。根據docs,只需使用Meteor的建議{{#if currentUser}}即可。其次,您可以使用模板系統。爲什麼不使用它來定義窗口小部件應該如何顯示在頁面上,而不是在您的rendered函數中以編程方式插入DOM節點?你爲什麼使用async?嘗試通過儘可能少的模板儘可能地通過模板儘可能地完成任務,然後放入助手只是爲了實現模板不能完成的任務。您通常也不需要深入Meteor的回調和異步代碼。

+0

是的你好,現在我使用currentUser,我刪除了異步循環,但是我的問題總是一樣的。當我登錄我的用戶時,loginWithPassword不起作用,並且logginIn方法保持不變。 我必須編輯我的代碼以重新加載頁面... – Ricklemer

+0

流星中沒有頁面重新加載。 '{{#unit currentUser}} {{> yourLoginTemplate}} {{else}} {{> yourHomePageTemplate}} {{/除非}}'。 –

0

嘗試重置您的項目與流星重置命令我重命名文件後有類似的問題,現在爲我工作與{{#if currentUser}}助手。