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.
This commit is contained in:
Dan Stillman 2016-05-17 13:35:52 -04:00
parent dd9cc40c16
commit 7192929ac3
2 changed files with 33 additions and 4 deletions

View File

@ -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) {

View File

@ -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]);
}
}