From ae949bdd3846688a4eaceee13ef3711bd37f4324 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 13 Aug 2008 02:21:49 +0000 Subject: [PATCH] Proper updating of cached parent collections --- .../content/zotero/xpcom/data/collection.js | 49 ++++++++++++++----- .../content/zotero/xpcom/data/collections.js | 34 +++++++++++++ 2 files changed, 71 insertions(+), 12 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/collection.js b/chrome/content/zotero/xpcom/data/collection.js index 8a182733c..5defa0e4b 100644 --- a/chrome/content/zotero/xpcom/data/collection.js +++ b/chrome/content/zotero/xpcom/data/collection.js @@ -179,12 +179,6 @@ Zotero.Collection.prototype.hasChildItems = function() { return !!(parseInt(this._hasChildItems)); } -Zotero.Collection.prototype.refreshChildCollections = function () { - this._hasChildCollections = undefined; - this._childCollectionsLoaded = false; -} - - /** * Check if collection exists in the database * @@ -319,7 +313,12 @@ Zotero.Collection.prototype.save = function () { Zotero.Collections.unload(oldID); Zotero.Notifier.trigger('id-change', 'collection', oldID + '-' + this.id); - // update caches + // Update child collections that have cached the previous id + var sql = "SELECT collectionID FROM collections WHERE parentCollectionID=?"; + var children = Zotero.DB.columnQuery(sql, this.id); + if (children) { + Zotero.Collections.refreshParents(children); + } } var isNew = !this.id || !this.exists(); @@ -510,12 +509,9 @@ Zotero.Collection.prototype.save = function () { Zotero.Notifier.trigger('modify', 'collection', this.id, this._previousData); } - // Refresh child collection counts + // Invalidate cached child collections if (parentIDs) { - for each(var id in parentIDs) { - var col = Zotero.Collections.get(id); - col.refreshChildCollections(); - } + Zotero.Collections.refreshChildCollections(parentIDs) } return this.id; @@ -982,6 +978,35 @@ Zotero.Collection.prototype._loadChildItems = function() { } +/** + * Note: This is called by Zotero.Collections.refreshParent() + * + * @private + */ +Zotero.Collection.prototype._refreshParent = function () { + if (!this.id) { + throw ("Cannot call Zotero.Collection._refreshParent() on unsaved collection"); + } + + var sql = "SELECT parentCollectionID FROM collections " + + "WHERE collectionID=?"; + this._parent = Zotero.DB.valueQuery(sql, this.id); +} + + +/** + * Invalid child collection cache + * + * Note: This is called by Zotero.Collections.refreshChildCollections() + * + * @private + */ +Zotero.Collection.prototype._refreshChildCollections = function () { + this._hasChildCollections = undefined; + this._childCollectionsLoaded = false; +} + + Zotero.Collection.prototype._generateKey = function () { return Zotero.ID.getKey(); } diff --git a/chrome/content/zotero/xpcom/data/collections.js b/chrome/content/zotero/xpcom/data/collections.js index 1b489f2df..44f2441ca 100644 --- a/chrome/content/zotero/xpcom/data/collections.js +++ b/chrome/content/zotero/xpcom/data/collections.js @@ -94,6 +94,40 @@ Zotero.Collections = new function() { } + /** + * Refresh cached parents in specified collections, skipping + * any that aren't loaded + * + * @param {Integer|Integer[]} ids One or more itemIDs + */ + this.refreshParents = function (ids) { + ids = Zotero.flattenArguments(ids); + + for each(var id in ids) { + if (_collections[id]) { + _collections[id]._refreshParent(); + } + } + } + + + /** + * Invalidate child collection cache in specified collections, skipping + * any that aren't loaded + * + * @param {Integer|Integer[]} ids One or more itemIDs + */ + this.refreshChildCollections = function (ids) { + ids = Zotero.flattenArguments(ids); + + for each(var id in ids) { + if (_collections[id]) { + _collections[id]._refreshChildCollections(); + } + } + } + + function reload(id) { if (!_collectionsLoaded) { this.reloadAll();