【问题标题】:Meteor simple-todos app - How can I show incomplete task count only for current user?Meteor simple-todos 应用程序 - 如何仅为当前用户显示不完整的任务计数?
【发布时间】:2017-09-03 15:58:42
【问题描述】:

我正在完成this Meteor 教程,并且我已经更新了 imports/ui/task.html 以仅向创建它们的用户显示任务,如下所示:

<template name="task">
{{#if isOwner}}
  <li class="{{#if checked}}checked{{/if}} {{#if private}}private{{/if}}">
    <button class="delete">&times;</button>

<input type="checkbox" checked="{{checked}}" class="toggle-checked" />

  <span class="text">{{text}}</span>

  </li>
    {{/if}}
</template>

但是,我仍然有显示所有用户任务的不完整计数,我想将其更改为仅登录的用户。这是我认为需要更改的 imports/ui/body.js 的一部分.

Template.body.helpers({
  tasks() {
  const instance = Template.instance();
if (instance.state.get('hideCompleted')) {
  // If hide completed is checked, filter tasks
  return Tasks.find({ checked: { $ne: true } }, { sort: { createdAt: -1 } });
}
// Otherwise, return all of the tasks
    // Show newest tasks at the top. This is the meat of the thing!
return Tasks.find({}, { sort: { createdAt: -1 } });
  },
incompleteCount() {
return Tasks.find({ checked: { $ne: true } }).count();
  },
});

【问题讨论】:

  • 欢迎来到 SO。请从阅读帮助部分开始,尤其是:stackoverflow.com/help/mcve。不要指望这里的人们阅读本教程只是为了了解您遇到的问题。说明问题中的问题。另外,向我们展示您已经尝试过的内容,而不是您认为必须更改的内容。

标签: meteor


【解决方案1】:

您只需过滤本教程中用于将任务与用户关联的ownerId

incompleteCount() {
  return Tasks.find({ ownerId: Meteor.userId(), checked: { $ne: true } }).count();
},

请注意,当您使用这样的多个条件时,它们会被隐式地与。

【讨论】:

    猜你喜欢
    • 2016-03-20
    • 2012-07-05
    • 2017-08-07
    • 2015-10-22
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 2018-06-17
    相关资源
    最近更新 更多