Question about Tutorial


#1

I have a question about the Tutorial Todo app, see https://www.meteor.com/tutorials/blaze/publish-and-subscribe.

The part I struggle with was added in chapter 10. Publish and subscribe, but does not have to do with this topic at all.

Anyhow, so in general, whenever a new task is added in our Todo App, the following fields are created:

  • text
  • createdAt
  • owner
  • username

See insert method in imports/api/tasks.js:

    Tasks.insert({
      text,
      createdAt: new Date(),
      owner: this.userId,
      username: Meteor.users.findOne(this.userId).username,
    });

Now the task is rendered and as such instantly shows it’s status to be either “public” or “private”, based on the field “private” which can be true or false, see file imports/ui/task.html:

    {{#if isOwner}}
      <button class="toggle-private">
        {{#if private}}
          Private
        {{else}}
          Public
        {{/if}}
      </button>
    {{/if}}

However, how is this possible even? The helper function {{private}} is nowhere defined and there yet does not even exist a “private” field inside our Mongo collection?!

Thanks in advance.


#2

Mustache tags can be used for both helper functions and properties. They first check if there’s a helper function with that name, then a property. Blaze is nice in that it allows you to be lazy. When there is no “private” field, it returns as undefined, which is a falsy value for the if-statement :slight_smile: