-1

Here is a sample of the code I'm working on:

I'm able to create the localstorage object and I'm able to save to it.

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

However, I'm not able to delete the localstorage entry.

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();
});

Here is a js fiddle of the app: https://jsfiddle.net/brettdavis4/3sy61zaj/1/

4

1 に答える 1

3

コレクションからIDで既存のモデルを取得し、次にdestroy()それを取得する必要があります。コレクションと localStorage からそれを削除します。

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

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

于 2016-03-09T00:32:33.637 に答える