-1

这是我正在处理的代码示例:

我能够创建本地存储对象,并且能够保存到它。

var FoodJournalDay = Backbone.Collection.extend({
    model: Food,
    localStorage: new Backbone.LocalStorage("foodjournal")
});

但是,我无法删除本地存储条目。

var App = Backbone.View.extend({

    el: 'body',

    events: {
        "input #searchBox" : "prepCollection",
        "click #listing li" : "track",
        "click #save": "saveClicked",
        "click #savefoodday": "savefoodClicked",        
        "click .destroy": "destroy"
    },

    initialize: function () {

        this.model = new SearchList();
        this.foods = new AllFoods();
        this.journal = new FoodJournalDay();

        this.model.on('destroy', this.remove, this);
        this.listenTo(this.journal, 'destroy', this.renderfoods);

        this.foods.fetch();

    },

    saveClicked: function() {
        this.listenTo(this.journal, 'add', this.renderfoods);        

        this.journal.create(new Food({
            id: foodid,
            title: item_name,
            brand: brand_name,
            calories: calorieAmt,
            fat: fatAmt,
            sodium: sodiumAmt,
            protein: proteinAmt,
            carbs: carbsAmt,
            sugar: sugarAmt,
            html: trackedhtml
        }));
    },

    destroy: function (e) {

        var model = new FoodJournalDay({ id: id });
        this.journal.remove();
    },



    var app = new App();
});

这是应用程序的 js fiddle:https ://jsfiddle.net/brettdavis4/3sy61zaj/1/

4

1 回答 1

3

您需要通过 id 从集合中获取现有模型,然后获取destroy()它。这将从集合和本地存储中删除它。

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

https://jsfiddle.net/guanzo/3sy61zaj/2/

于 2016-03-09T00:32:33.637 回答