Content migrated by re-rendering currentUser when updating user

I use Meteor and have a problem when my content is re-displayed, when I do not want it.

I have my main content wrapped in an currentUserif statement, which I think is pretty standard.

{{#if currentUser}}
  {{> content}}
{{/if}}

The problem with this is that my content template is re-displayed when the user object is updated. Is there any way around this? I do not refer to users anywhere inside the content template.

Thank!

Here is an example application to replicate my problem:

HTML

<head>
  <title>Render Test</title>
</head>

<body>
  {{loginButtons}}

  {{> userUpdate}}

  {{#if currentUser}}
    {{> content}}
  {{/if}}
</body>

<template name="userUpdate">
  <p>
    <input id="updateUser" type="button" value="Update User Value" />
    User last update: <span id="lastUpdated">{{lastUpdated}}</span>
  </p>
</template>

<template name="content">
  <p>Render count: <span id="renderCount"></span></p>
</template>

Javascript

if (Meteor.isClient) {
  Meteor.startup(function() {
    Session.set("contentRenderedCount", 0);
  });

  Template.content.rendered = function() {
    var renderCount = Session.get("contentRenderedCount") + 1;
    Session.set("contentRenderedCount", renderCount);
    document.getElementById("renderCount").innerText = renderCount;
  };

  Template.userUpdate.events = {
    "click #updateUser": function() {
      Meteor.users.update({_id: Meteor.userId()}, {$set: {lastActive: new Date()}});
    }
  };

  Template.userUpdate.lastUpdated = function() {
    return Meteor.user().lastActive;
  };

}

if (Meteor.isServer) {
  Meteor.users.allow({
    'update': function () {
      return true; 
    }
  });
}

Update: . , " ", . , {{#if currentUser}}. , , , 1.

, accounts-ui accounts-password .

+5
1

, . {{currentUser}} Meteor.user(), , . , , , .

, , /, - :

Meteor.autorun(function() {
    Session.set("meteor_loggedin",!!Meteor.user());
});

Handlebars.registerHelper('session',function(input){
    return Session.get(input);
});

html

{{#if session "meteor_loggedin"}}
    {{> content}}
{{/if}}    
+8

All Articles