The javascript one seems pretty simple, just localStorage.clear().
Is there anything similar to that for the backbone localstorage, and if not, can someone point me
Just iterating over the collection and calling destroy on each element is not save in any case. The reason is iterating over collection modified at the same time can produce unexpected results.
It is better to first clone the collection and iterate over this clone. Checkout this.each not iterating through collection correctly for further details.
Example:
_.chain(Todos.models).clone().each(function(model){
console.log('deleting model ' + model.id);
model.destroy();
});
I know this kind of feels like grave digging, but i've been looking for a solution to this for a while now and none of the above snippets seemed to work for me. I always ended up having the size of the collection decreased by half, no matter how i tried it.
So after a decent amount of fiddling, i came up with this:
var length = collection.length;
for (var i = 0; i < length; i++) {
collection.at(0).destroy();
}
Backbone is removing items "on the fly", so if you have 40 items you won't be able to delete the 21. item because there are only 20 items left. Strangely enough this also seems to affect the collection.each() function which really seems like a bug to me..
If you don't want to do this programmatically, you can always open up Developer Mode (F12), navigate to the Resources tab, select "Local Storage" from the left pane and delete the records.
Update: In the newer versions of Chrome, the Resources tab has been replaced with the Application tab.
Few ways you can do this from the Collection, but whichever way you choose, you have to call destroy on each model, which will run sync and destroy it on both the client-side and server-side (which localStorage is acting as).
collection.each(function(model) {
model.destroy();
}
)
Update
Per comments, doesn't look like this works anymore. Since this is still marked as the answer, including answer that should work below, per skcin7
.
while ((model=collection.shift()))
{ model.destroy();
}