Commit 62c918ae authored by Tristan Cavelier's avatar Tristan Cavelier

localstorage.js remove method improved

parent b5f2b208
...@@ -57,6 +57,20 @@ var newLocalStorage = function (spec, my) { ...@@ -57,6 +57,20 @@ var newLocalStorage = function (spec, my) {
} }
}; };
/**
* Checks if an object has no enumerable keys
* @method objectIsEmpty
* @param {object} obj The object
* @return {boolean} true if empty, else false
*/
priv.objectIsEmpty = function (obj) {
var k;
for (k in obj) {
return false;
}
return true;
};
// ===================== overrides ====================== // ===================== overrides ======================
that.serialized = function () { that.serialized = function () {
return { return {
...@@ -225,16 +239,19 @@ var newLocalStorage = function (spec, my) { ...@@ -225,16 +239,19 @@ var newLocalStorage = function (spec, my) {
that.remove = function (command) { that.remove = function (command) {
setTimeout (function () { setTimeout (function () {
var doc; var doc;
doc = localstorage.getItem(
priv.localpath + "/" + command.getDocId());
if (typeof command.getAttachmentId() === "string") { if (typeof command.getAttachmentId() === "string") {
// seeking for an attachment // seeking for an attachment
doc = localstorage.getItem(
priv.localpath + "/" + command.getDocId());
localstorage.deleteItem( localstorage.deleteItem(
priv.localpath + "/" + command.getDocId() + "/" + priv.localpath + "/" + command.getDocId() + "/" +
command.getAttachmentId()); command.getAttachmentId());
// remove attachment from document // remove attachment from document
if (typeof doc["_attachments"] !== "undefined") { if (typeof doc["_attachments"] === "object") {
delete doc["_attachments"][command.getAttachmentId()]; delete doc["_attachments"][command.getAttachmentId()];
if (priv.objectIsEmpty(doc["_attachments"])) {
delete doc["_attachments"];
}
localstorage.setItem( localstorage.setItem(
priv.localpath + "/" + command.getDocId(), priv.localpath + "/" + command.getDocId(),
doc); doc);
...@@ -245,8 +262,21 @@ var newLocalStorage = function (spec, my) { ...@@ -245,8 +262,21 @@ var newLocalStorage = function (spec, my) {
}); });
} else { } else {
// seeking for a document // seeking for a document
var attachment_list = [], i;
if (typeof doc["_attachments"] === "object") {
// prepare list of attachments
for (i in doc["_attachments"]) {
attachment_list.push(i);
}
}
localstorage.deleteItem( localstorage.deleteItem(
priv.localpath + "/" + command.getDocId()); priv.localpath + "/" + command.getDocId());
// delete all attachments
for (i = 0; i < attachment_list.length; i += 1) {
localstorage.deleteItem(
priv.localpath+"/"+command.getDocId()+"/"+
attachment_list[i]);
}
that.success({ that.success({
"ok": true, "ok": true,
"id": command.getDocId() "id": command.getDocId()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment