0

作为一个个人项目,我正在研究backbone.js 中的食物计算器。我通过backbone.localstorage.js 存储食物条目。我的大部分工作正常。我遇到的唯一问题是在我保存我的项目并点击刷新后,我无法删除这些项目。如果我添加一个项目,我可以在删除后直接删除它。

这是我初始化所有内容的函数:

initialize: function () {        
        this.model = new SearchList();
        this.foods = new AllFoods();
        this.journal = new FoodJournalDay();
        this.prepCollection = _.debounce(this.prepCollection, 1000);
        this.$totalcal = $('#totalcal span');
        this.$totalcalfat = $('#totalcalfat span');
        this.$totalprotein = $('#totalprotein span');
        this.$totalcarb = $('#totalcarb span');
        this.$totalfat = $('#totalfat span');
        this.$totalsugar = $('#totalsugar span');
        this.$totalsodium = $('#totalsodium span');
        this.$totalpotassium = $('#totalpotassium span');
        this.$list = $('#listing');
        this.$breakfast = $('#breakfast');
        this.$lunch = $('#lunch');
        this.$dinner = $('#dinner');
        this.$snack = $('#snack');
        this.$tracked = $('#tracked');

        this.listenTo(this.journal, 'all', _.debounce(this.render, 0));
        this.model.on('destroy', this.remove, this);
        this.listenTo(this.journal, 'destroy', this.renderfoods);        
        this.foods.fetch();        

        var myNotes = new FoodJournalDay();
        myNotes.fetch();

        myNotes.models.forEach(function(model){

            var mealli = model.get("html");

            var calcount = parseFloat(model.get("calories"));
            var proteincount = parseFloat(model.get("protein"));
            var carbscount = parseFloat(model.get("carbs"));
            var fatcount = parseFloat(model.get("fat"));
            var sugarcount = parseFloat(model.get("sugar"));

            totalcal += calcount;
            totalprotein += proteincount;
            totalcarb += carbscount;
            totalfat += fatcount;
            totalsugar += sugarcount;

            switch (model.get("meal")) {

                case 'Breakfast':
                    $('#breakfast').append(mealli);
                    break;
                case 'Lunch':
                        $('#lunch').append(mealli);
                    break;
                case 'Dinner':
                        $('#dinner').append(mealli);
                    break;
                case 'Snack':
                    $('#snack').append(mealli);
                    break;
                default:
                    //alert("Please select a meal!");
            }


            $('#totalcal span').html(totalcal);
            $('#totalprotein span').html(totalprotein);
            $('#totalcarb span').html(totalcarb);
            $('#totalfat span').html(totalfat);
            $('#totalsugar span').html(totalsugar);
        });
    },

这是我的删除功能:

destroy: function (e) {
        var $li = $(e.currentTarget).closest('li');
        var id = $li.attr('data-id');
        $li.remove();

        console.log("li id: " + id);
        console.log(this.journal);

        this.journal.get(id).destroy();
    },

我对我正在做的事情做了一个小提琴: https ://jsfiddle.net/brettdavis4/ynm2sse9/2/

谢谢!

4

1 回答 1

1

我回来参加第 2 轮:p。

您向视图添加了 2 个相同的集合。你应该坚持一个。

原来你有this.journal = new FoodJournalDay();作为你的收藏。并且在我之前的回答中this.journal.get(id).destroy();成功删除了该项目。但是,在您的新代码中,您创建了另一个集合var myNotes = new FoodJournalDay();。这是相当没有意义的,因为您已经有一个FoodJournalDay()集合分配给this.journal.

您填充myNotes了 a fetch(),因此您this.journal留空。由于this.journal正在删除,它会尝试查找具有 id 的模型this.journal.get(id)。这会返回undefined,因为没有带有 that的模型id,实际上集合中根本没有模型。这就是您收到错误消息的原因Cannot read property 'destroy' of undefined

当某些东西不起作用时,我建议遵循程序的流程并在console.log()各处放置语句,检查您的集合、模型的内容,并查看是否可以发现任何意外行为。

https://jsfiddle.net/guanzo/1mq7mdm1/

我换了

var myNotes = new FoodJournalDay();
myNotes.fetch();

myNotes.models.forEach(function(model){

有了这个。使用原始集合。

this.journal.fetch();
this.journal.models.forEach(function(model){
于 2016-03-11T00:45:36.427 回答