I'm making a website with Backbone and Marionette and in this website I'm making a shopping cart.
But, in the cart, I don't want to create an endpoint to save the items if the user is unlogged. I want to save only in localStorage.
I'm trying to make this with Backbone.localStorage but I cannot make this.
App.Collections.Cart = Backbone.Collection.extend({
model: GroovecollabWebsite.Models.Category,
localStorage: new Backbone.LocalStorage("localCart")
});
I want to make this:
var collection = new App.Collections.Cart();
collection.add({/* My model here */}); // Run event to sync with localStorage
collection.remove(<id>); // Run event to sync with localStorage
How can I do this?
Thanks!
If you want to save your collection on changes, you can add it in the initializer in your collection:
initialize: function() {
this.on('add remove', function() {
this.sync();
});
}