Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

backbone.js - How to clear the backbone localstorage

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 in the right direction on how to do it.

I was thinking about doing something like this:

localStorage.each(localStorage.delete(this))

except this wouldn't point to that element would it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...