Garlic.js
Garlic.js copied to clipboard
.garlic('destroy') doesn't work from success page
Garlic.js is awesome, however I found a limitation -
When a user fills out a form successfully they are taken to a success page
from there my code tries to destroy the local storage data
$('#form').garlic('destroy');
however the form data remains in local storage.
I think because the server has changed the page the reference to garlic's local storage key's are no longer matching.
My work-around is a little messy but this is what i used
function destroyGarlic() {
var aGarlicKeys = [];
for ( var i = 0, len = localStorage.length; i < len; ++i ) {
if ( localStorage.key(i).indexOf("garlic") >= 0 ) {
aGarlicKeys.push( localStorage.key(i) );
}
};
jQuery.each(aGarlicKeys, function(index, item) {
localStorage.removeItem(item);
});
}
destroyGarlic();
console.log('data destroyed.');
Bassically I search for all keys in local storage containing the string "garlic", push them into an array and then use the array to remove the garlic keys from local storage.
It would be great if garlic.destroy();
could be flexible like this.
Hope this helps someone
Cheers, Nick