commit 7192929ac3434d9721809a1e9ca8533e997ecb36
parent dd9cc40c16ef9171b645a2fd021a5ac586714678
Author: Dan Stillman <dstillman@zotero.org>
Date: Tue, 17 May 2016 13:35:52 -0400
Add Zotero.DataObjects.prototype.loadDataTypes(objects, dataTypes)
Bulk-loads data for objects that are potentially in different libraries. This
would generally be used to load necessary data for cross-library search
results, since those results might include objects in libraries that haven't
yet been loaded.
Diffstat:
2 files changed, 33 insertions(+), 4 deletions(-)
diff --git a/chrome/content/zotero/xpcom/data/dataObject.js b/chrome/content/zotero/xpcom/data/dataObject.js
@@ -679,7 +679,7 @@ Zotero.DataObject.prototype._requireData = function (dataType) {
* @param {Promise}
*/
Zotero.DataObject.prototype.loadDataType = function (dataType, reload) {
- return this._ObjectsClass._loadDataType(dataType, this.libraryID, [this.id]);
+ return this._ObjectsClass._loadDataTypeInLibrary(dataType, this.libraryID, [this.id]);
}
Zotero.DataObject.prototype.loadAllData = Zotero.Promise.coroutine(function* (reload) {
diff --git a/chrome/content/zotero/xpcom/data/dataObjects.js b/chrome/content/zotero/xpcom/data/dataObjects.js
@@ -387,12 +387,41 @@ Zotero.DataObjects.prototype.getObjectVersions = Zotero.Promise.coroutine(functi
/**
+ * Bulk-load data type(s) of given objects if not loaded
+ *
+ * This would generally be used to load necessary data for cross-library search results, since those
+ * results might include objects in libraries that haven't yet been loaded.
+ *
+ * @param {Zotero.DataObject[]} objects
+ * @param {String[]} dataTypes
+ * @return {Promise}
+ */
+Zotero.DataObjects.prototype.loadDataTypes = Zotero.Promise.coroutine(function* (objects, dataTypes) {
+ for (let dataType of dataTypes) {
+ let typeIDsByLibrary = {};
+ for (let obj of objects) {
+ if (obj._loaded[dataType]) {
+ continue;
+ }
+ if (!typeIDsByLibrary[obj.libraryID]) {
+ typeIDsByLibrary[obj.libraryID] = [];
+ }
+ typeIDsByLibrary[obj.libraryID].push(obj.id);
+ }
+ for (let libraryID in typeIDsByLibrary) {
+ yield this._loadDataTypeInLibrary(dataType, parseInt(libraryID), typeIDsByLibrary[libraryID]);
+ }
+ }
+});
+
+
+/**
* Loads data for a given data type
* @param {String} dataType
* @param {Integer} libraryID
* @param {Integer[]} [ids]
*/
-Zotero.DataObjects.prototype._loadDataType = Zotero.Promise.coroutine(function* (dataType, libraryID, ids) {
+Zotero.DataObjects.prototype._loadDataTypeInLibrary = Zotero.Promise.coroutine(function* (dataType, libraryID, ids) {
var funcName = "_load" + dataType[0].toUpperCase() + dataType.substr(1)
// Single data types need an 's' (e.g., 'note' -> 'loadNotes()')
+ ((dataType.endsWith('s') || dataType.endsWith('Data') ? '' : 's'));
@@ -436,7 +465,7 @@ Zotero.DataObjects.prototype.loadAll = Zotero.Promise.coroutine(function* (libra
let dataTypes = this.ObjectClass.prototype._dataTypes;
for (let i = 0; i < dataTypes.length; i++) {
- yield this._loadDataType(dataTypes[i], libraryID, ids);
+ yield this._loadDataTypeInLibrary(dataTypes[i], libraryID, ids);
}
Zotero.debug(`Loaded all ${this._ZDO_objects} in ${library.name} in ${new Date() - t} ms`);
@@ -679,7 +708,7 @@ Zotero.DataObjects.prototype.reload = Zotero.Promise.coroutine(function* (ids, d
typeIDsByLibrary[obj.libraryID].push(id);
}
for (let libraryID in typeIDsByLibrary) {
- yield this._loadDataType(dataType, parseInt(libraryID), typeIDsByLibrary[libraryID]);
+ yield this._loadDataTypeInLibrary(dataType, parseInt(libraryID), typeIDsByLibrary[libraryID]);
}
}