javascript - 在 Meteor.js 中删除类别

标签 javascript meteor

我正在用 Meteor 编写一个小应用程序。我完成了添加类别的代码,但我不知道如何删除类别。 我的 HTML 代码是:

<template name="categories">
<h2>Stored Category</h2>
<div id="categories" class="btn-group">
    {{#if new_cat}}
        <div id="category">
            <input type="text" id="add-category" value=""/>
        </div>      
    {{else}}            
        <div class="category btn btn-inverse" id="btnNewCat">&plus;</div>
    {{/if}}

    {{#if del_cat}}
        <div id="category">
            <input type="text" id="del-category" value=""/>
        </div>      
    {{else}}            
        <div class="category btn btn-inverse" id="btnDelCat">&minus;</div>
    {{/if}}

    {{#each lists}}
        <div class="category btn btn-inverse">
            {{Category}}
        </div>
    {{/each}}
</div>

显然,添加类别的按钮是 btnNewCat,删除类别的按钮是 btnDelCat。 Javascript 代码是:

    Template.categories.lists = function () {
    return lists.find({}, {sort: {Category: 1}});
};

Session.set('adding_category', false);
Session.set('deleting_category', false);

Template.categories.new_cat = function () {
    return Session.equals('adding_category',true);
};

Template.categories.del_cat = function(){
    return Session.equals('deleting_category',true);
};

Template.categories.events({
    'click #btnNewCat': function (e, t) {
        Session.set('adding_category', true);
        Meteor.flush();
        focusText(t.find("#add-category"));
    },
    'keyup #add-category': function (e,t){
        if (e.which === 13)
        {
            var catVal = String(e.target.value || "");
            if (catVal)
            {
                lists.insert({Category:catVal});
                Session.set('adding_category', false);
            }
        }
    },
    'focusout #add-category': function(e,t){
        Session.set('adding_category',false);
    },

    'click #btnDelCat': function (e, t) {
        Session.set('deleting_category', true);
        Meteor.flush();
        focusText(t.find("#del-category"));
    },
    'keyup #del-category': function (e,t){
        if (e.which === 13)
        {
            var catVal = String(e.target.value || "");
            if (catVal)
            {
                alert(catVal);
                lists.remove({Category:catVal});
                Session.set('deleting_category', false);
            }
        }
    },
    'focusout #del-category': function(e,t){
        Session.set('deleting_category',false);
    }

我尝试使用 lists.remove({Category:catVal}); 删除。这是行不通的。我哪里做错了?

谢谢,

eb_cj

最佳答案

您只能通过_id 删除客户端中的项目,因此您需要执行以下操作:

var removeCat = lists.findOne({Category:catVal});
if (removeCat) lists.remove(removeCat._id);

在浏览器控制台中尝试这些东西应该表明您的代码原则上是否有效;如果你尝试执行 lists.remove({Category:catVal}),它应该给你:

Error: Not permitted. Untrusted code may only remove documents by ID. [403]

但上面的代码应该可以工作。

关于javascript - 在 Meteor.js 中删除类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21118788/

相关文章:

JavaScript 正则表达式 匹配之前的任何内容

javascript - Meteor.wrapAsync 和 mailchimp-api-v3 出现问题

javascript - Meteor:单击时将模板渲染到 DOM

javascript - 添加数据后,我从 Firestore 获取重复数据。在 React Native 中

javascript - typescript 错误 - 类型上不存在属性 'permission'

javascript - meteor 铁:router transitions

javascript - meteor JS : What is the best way to store states for a specific template instance?

node.js - Meteor NGinx 网关错误

php - 在 PHP 中使用多维数组生成有效的 JSONP

javascript - jquery验证错误信息没有隐藏,为什么