0

Be patience I'm new at meteor.

I'm following the good getting start tutorial and just playing around.

Now I want to display only the todos belong to the current user.

I realized handlebar doesn't have an if expression statement and I bumped into this useful link

Logical operator in a handlebars.js {{#if}} conditional

I've tried with:

simple-todo.html

<template name="task">
  {{#ifCond  owner  Meteor.userId() }}
    <li class="{{#if checked}}checked{{/if}}">
      <button class="delete">&times;</button>
      <input type="checkbox" checked="{{checked}}" class="toggle-checked" />
      <span class="text"><strong>{{username}}</strong> - {{text}}</span>
    </li>
  {{/ifCond}}
</template>

simple-todo.js

Handlebars.registerHelper('ifCond', function(v1, v2, options) {
  if(v1 === v2) {
    return options.fn(this);
  }
  return options.inverse(this);
});

1) is it the right place to set an handlebar helper ?

but I don't see any todo :(

Can you help me, please ?

Community
  • 1
  • 1
Whisher
  • 31,320
  • 32
  • 120
  • 201
  • what is `owner` in `{{#ifCond owner Meteor.userId() }}` ? – MatiK Jan 06 '15 at 10:37
  • Handlebars are removed in the meteor latest versions , may i know which version are you working with..?? – sitaram9292 Jan 06 '15 at 10:39
  • @MatiK is a document field Tasks.insert({ text: text, createdAt: new Date(), // current time owner: Meteor.userId(), // _id of logged in user username: Meteor.user().username // username of logged in user }); – Whisher Jan 06 '15 at 11:00
  • @sitaram9292 Meteor 1.0.2.1 – Whisher Jan 06 '15 at 11:02

2 Answers2

1

From meteor docs:

Note that you must use the equals method (or EJSON.equals) to compare them; the === operator will not work. If you are writing generic code that needs to deal with _id fields that may be either strings or ObjectIDs, use EJSON.equals instead of === to compare them.

http://docs.meteor.com/#/full/mongo_object_id

MatiK
  • 573
  • 1
  • 7
  • 21
  • thanks for the point but like this https://gist.github.com/whisher/16e87f3106c4c308d90b I've got simple-todos.html:34: Expected space ....equals Meteor.userId() }} – Whisher Jan 06 '15 at 12:46
0

Just write a helper to display current users records only.

For displaying records to current user.

{{#if currentusertodos}}
<li class="{{#if checked}}checked{{/if}}">
  <button class="delete">&times;</button>
  <input type="checkbox" checked="{{checked}}" class="toggle-checked" />
  <span class="text"><strong>{{username}}</strong> - {{text}}</span>
</li>
{{/if}}

You must have userId in the todos collection contains _id of the current user.

Template.task.helpers({
currentusertodos:function(){

  return todos.find({userId:Meteor.userId()}).count();
 }
});

In this way you can do it

I hope this is useful for you.

sitaram9292
  • 171
  • 8