diff --git a/chrome/content/zotero/xpcom/data/dataObject.js b/chrome/content/zotero/xpcom/data/dataObject.js index 38ad43bc3..062b78065 100644 --- 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 index b042149a9..32fd209bf 100644 --- a/chrome/content/zotero/xpcom/data/dataObjects.js +++ b/chrome/content/zotero/xpcom/data/dataObjects.js @@ -386,13 +386,42 @@ 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]); } }