简体   繁体   English

流星待办事项的关联列表和任务

[英]Associate Lists and Tasks in Meteor todo

I'm building the todo application from the Meteor tutorial and continue it. 我正在从Meteor教程构建todo应用程序,然后继续进行。 I'm building some lists based on the task model, but I don't know how to join them and say when I click on one list, I want all the tasks from this one. 我正在基于任务模型构建一些列表,但是我不知道如何加入它们,并说当我单击一个列表时,我希望从该列表中获得所有任务。

For the moment, I have the Tasks.js with: 目前,我的Tasks.js具有:

    'tasks.insert'(text, privacy, priority, listId) {
        ...

        Tasks.insert({
            text,

            listId: listId,

            owner: this.userId,
            username: Meteor.users.findOne(this.userId).username,
        });
    },

Body.js Body.js

   Template.body.events({
      'submit .new-task' (event) {
           event.preventDefault();

    const listId = ???

    const target = event.target;
    const text = target.text.value;
    ...
    Meteor.call('tasks.insert', text, privacy, priority, listId);
    ...
},

And then where I display it: 然后在哪里显示它:

Template.body.helpers({
    tasks() {
        const instance = Template.instance();

        if (instance.state.get('hideCompleted')) {
            return Tasks.find({ checked: { $ne: true } }, { sort: Session.get("sort_order") });
        }
        return Tasks.find({}, { sort: Session.get("sort_order")});
    },

    lists() {
        return Lists.find({}, { sort: { createdAt: -1 } });
    },

I my body.html, I just display each items (lists and tasks) separately. 在我的body.html中,我只是分别显示每个项目(列表和任务)。 But the problem is I don't know how to make the relation between both ... 但是问题是我不知道如何建立两者之间的关系...

Can you help me please ? 你能帮我吗 ?

Thanks a lot 非常感谢

I see you are already using Session. 我看到您已经在使用Session。 Basically, you will use a Session variable that tracks what the list the user has selected, and then filter your tasks with that variable. 基本上,您将使用Session变量来跟踪用户选择的列表,然后使用该变量过滤任务。

In your body, where you're displaying your list names, add the list's id as an HTML attribute: 在您要显示列表名称的位置,将列表的ID作为HTML属性添加:

{{#each lists}}
  <a href='#' class='list-name' data-id='{{this._id}}'>
    {{this.name}}
  </a>
{{/each}}

Add an event for clicking on a list name that saves its id to a Session variable: 添加事件以单击列表名称,将其ID保存到Session变量中:

Template.body.events({
  'click .list-name' (event) {
    event.preventDefault();

    Session.set('listId', event.currentTarget.attr('data-id'))
  }
})

In your tasks helper, filter your query using the Session variable: tasks助手中,使用Session变量过滤查询:

return Tasks.find(
  { listId: Session.get('listId') },
  { sort: Session.get("sort_order") }
);

Let me know if anything could be more clear. 让我知道是否有任何更清楚的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM