从Meteor.js中的Array中删除Item [英] Remove Item from Array in Meteor.js

查看:45
本文介绍了从Meteor.js中的Array中删除Item的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Rulesets的集合 - 每个规则集都有一个规则数组。我有以下html显示每个规则集和每个规则:

I have a collection called Rulesets - each ruleset has an array of "rules". I have the following html which displays each ruleset and each rule:

<template name="rulesets">
    {{#each rulesets}}
        {{>rulesetSingle}}
    {{/each}}
</template>

<template name="rulesetSingle">
    {{#each rules}}
         <p class="rule-description">{{this}}
             <a href="#" class="rule-delete-btn">x</a>
         </p>
    {{/each}}
</template>

我希望能够在用户点击rule-delete-btn时删除该规则。$
我有以下javascript来执行此操作:

I want to be able to remove the rule when the user clicks the "rule-delete-btn".
I have the following javascript to do this:

Template.rulesetSingle.event({
    'click .rule-delete-btn': function(e){
        e.preventDefault();
        var array = Rulesets.rules;
        var index = array.indexOf(this);
        array.splice(index, 1);
    }
});

删除无效,因为数组声明没有提取有效数组。如何找到并存储包含this的数组,这是当前需要删除的规则?

The delete isn't working because the "array" declaration isn't pulling a valid array. How can I find and store the array that contains "this" which is the current rule that needs to be deleted?

推荐答案

假设你的 Rulesets.rules 数组是一个对象数组,如:

Assuming your Rulesets.rules array is an array of objects like :

Rulesets : { { title: 'title1', 
               rules : [ {name : 'rule1', description : 'description1'}, 
                         {name : 'rule2', description : 'description2'}
                       ]
             },
             { title: 'title2', 
               rules : [ {name : 'rule1', description : 'description1'}, 
                         {name : 'rule2', description : 'description2'}
                       ]
             } }

首先,在您的 rulesetSingle 模板,您必须将该特定文档的 _id 分配给< a> ; 喜欢:

First, in your rulesetSingle template, you must assign the _id of that particular document to the <a> like :

< a href =#name ={{../_ id}}class =rule-delete-btn> x< / a>

第二件事,您尝试使用删除规则条目Array.splice() ,这是不可能的,因为规则是集合中的文档。您必须更新查询 Rulesets.update()

Second thing, you are trying to remove rule entry using Array.splice(), this is not possible since the rules is a document inside the collection. You must do update query as Rulesets.update().

如果您已在服务器上完成 allow update ,则可以从事件处理程序中删除此数组条目,否则你必须通过传递规则 _id Meteor.call() c $ c>父文件。

If you have done allow update on the server, you can delete this array entry from within the event handler, otherwise you must do Meteor.call() by passing rule an the _id of parent document.

因此,事件处理程序将类似于:

So, the event handler will look something like :

 'click .rule-delete-btn': function(e) {
        e.preventDefault();
        var rule = this;
        var id = e.currentTarget.name;
        Meteor.call('removeRule', id, rule);
  }

在服务器上:

  Meteor.methods({
    removeRule: function(id, rule){
      Rulesets.update({_id: id}, {$pull : {rules : rule}});
    }
  });

这篇关于从Meteor.js中的Array中删除Item的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆