How to clear the backbone localstorage

前端 未结 4 509
栀梦
栀梦 2020-12-30 12:12

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

4条回答
  •  一生所求
    2020-12-30 12:39

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

提交回复
热议问题