commit 4450e12152aab2092112fc4741c8e3fa693c34e2
parent 0fb1d5866f22b66285ce8eae5ebd8eed0fca5cda
Author: Dan Stillman <dstillman@zotero.org>
Date: Wed, 13 Aug 2008 02:21:49 +0000
Proper updating of cached parent collections
Diffstat:
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
@@ -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
@@ -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();