Update collection cache after "Delete collection and items…"

Fixes #1314
This commit is contained in:
Dan Stillman 2017-09-13 01:01:36 -04:00
parent 5ec7c97f30
commit c442daedce
2 changed files with 18 additions and 5 deletions

View file

@ -569,6 +569,7 @@ Zotero.Collection.prototype._eraseData = Zotero.Promise.coroutine(function* (env
var descendents = this.getDescendents(false, null, true); var descendents = this.getDescendents(false, null, true);
var items = []; var items = [];
var libraryHasTrash = Zotero.Libraries.hasTrash(this.libraryID);
var del = []; var del = [];
var itemsToUpdate = []; var itemsToUpdate = [];
@ -586,19 +587,23 @@ Zotero.Collection.prototype._eraseData = Zotero.Promise.coroutine(function* (env
} }
// Descendent items // Descendent items
else { else {
// Delete items from DB // Trash/delete items
if (env.options.deleteItems) { if (env.options.deleteItems) {
del.push(descendents[i].id); del.push(descendents[i].id);
} }
else {
// If item isn't being removed or is just moving to the trash, mark for update
if (!env.options.deleteItems || libraryHasTrash) {
itemsToUpdate.push(descendents[i].id); itemsToUpdate.push(descendents[i].id);
} }
} }
} }
if (del.length) { if (del.length) {
if (Zotero.Libraries.hasTrash(this.libraryID)) { if (libraryHasTrash) {
yield this.ChildObjects.trash(del); yield this.ChildObjects.trash(del);
} else { }
// If library doesn't have trash, just erase
else {
Zotero.debug(Zotero.Libraries.getName(this.libraryID) + " library does not have trash. " Zotero.debug(Zotero.Libraries.getName(this.libraryID) + " library does not have trash. "
+ this.ChildObjects._ZDO_Objects + " will be erased"); + this.ChildObjects._ZDO_Objects + " will be erased");
let options = {}; let options = {};
@ -638,7 +643,7 @@ Zotero.Collection.prototype._eraseData = Zotero.Promise.coroutine(function* (env
env.deletedObjectIDs = collections; env.deletedObjectIDs = collections;
// Update collection cache for descendant items // Update collection cache for descendant items
if (!env.options.deleteItems) { if (itemsToUpdate.length) {
let deletedCollections = new Set(env.deletedObjectIDs); let deletedCollections = new Set(env.deletedObjectIDs);
itemsToUpdate.forEach(itemID => { itemsToUpdate.forEach(itemID => {
let item = Zotero.Items.get(itemID); let item = Zotero.Items.get(itemID);

View file

@ -53,6 +53,14 @@ describe("Zotero.Collection", function() {
yield collection.eraseTx(); yield collection.eraseTx();
assert.lengthOf(item.getCollections(), 0); assert.lengthOf(item.getCollections(), 0);
}); });
it("should clear collection from item cache in deleteItems mode", function* () {
var collection = yield createDataObject('collection');
var item = yield createDataObject('item', { collections: [collection.id] });
assert.lengthOf(item.getCollections(), 1);
yield collection.eraseTx({ deleteItems: true });
assert.lengthOf(item.getCollections(), 0);
});
}) })
describe("#version", function () { describe("#version", function () {