【问题标题】:Problems deleting a Meteor.js collection using template event listeners使用模板事件侦听器删除 Meteor.js 集合的问题
【发布时间】:2015-01-29 01:57:03
【问题描述】:

我对 Meteor 比较陌生,并且在弄清楚这一点时遇到了一些困难,所以也许有人可以让我知道我做错了什么。我在客户端/服务器可用的 Meteor (mongo) 集合中创建了一个项目列表。我想删除显示在删除按钮旁边的项目。到目前为止我已经做到了。我输入数据的第一部分工作正常,模板列表确实显示了我的所有数据:

在我的 html 文件中 (client/list.html)

<template name=List>
  <table class="table">
    <tr>
      <td>Item</td>
      <td>Description</td>
    </tr>
    <tr>
      <td>{{itemName}}</td>
      <td>{{description}}</td>
      <button class="btn" id="delete" type="submit">Delete</button>
    </tr>
</template>

关于js文件(client/list.js)

Template.quickList.events({
  'click #delete': function(e, t){
    cl = Lists.findOne(t.data);
    Lists.remove({_id: cl._id});
  }
});

注意:如果我尝试 console.log(t.data)

,我会在事件侦听器中(在 list.js 文件中)得到一个空值

不知道从这里去哪里。

【问题讨论】:

    标签: javascript mongodb templates events meteor


    【解决方案1】:

    模板的事件映射处理的事件将在this 关键字中提供模板的数据上下文。如果您的项目是数据上下文(如果您在没有帮助程序的情况下获得itemNamedescription),您可以在事件处理程序中使用this._id

    <tr>
      <td>{{itemName}}</td>
      <td>{{description}}</td>
      <button class="btn" id="delete" type="submit">Delete</button>
    </tr>
    

    随着事件:

    Template.quickList.events({
      'click #delete': function(event){
        Lists.remove(this._id);
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多