javascript - Remove Item from Array in Meteor.js -


i have collection called rulesets - each ruleset has array of "rules". have following html displays each ruleset , 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> 

i want able remove rule when user clicks "rule-delete-btn".
have following javascript 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);     } }); 

the delete isn't working because "array" declaration isn't pulling valid array. how can find , store array contains "this" current rule needs deleted?

assuming rulesets.rules array array of objects :

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

first, in rulesetsingle template, must assign _id of particular document <a> :

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

second thing, trying remove rule entry using array.splice(), not possible since rules document inside collection. must update query rulesets.update().

if have done allow update on server, can delete array entry within event handler, otherwise must meteor.call() passing rule _id of parent document.

so, event handler :

 'click .rule-delete-btn': function(e) {         e.preventdefault();         var rule = this;         var id = e.currenttarget.name;         meteor.call('removerule', id, rule);   } 

on server:

  meteor.methods({     removerule: function(id, rule){       rulesets.update({_id: id}, {$pull : {rules : rule}});     }   }); 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -