Return object instead of array from Zotero.DataObjects.getLibraryAndKeyFromID()

Object contains 'libraryID' and 'key' properties

This is due to changed array destructuring behavior in Firefox. Previously,
`var [foo, bar] = maybeArrayMaybeFalse()` always worked, leaving foo and bar
undefined if the function returned false. Now (with ES6, I assume), if the
function returns false it results in a "false[Symbol.iterator] is not a
function" error. But `var {libraryID, key} = false` works as expected, leaving
both values undefined, so instead we can just return an object with those
properties from getLibraryAndKeyFromID(). To assign to different variables, use
`var {libraryID, key: parentItemKey} = ...`.
This commit is contained in:
Dan Stillman 2015-04-25 02:18:11 -04:00
parent 90f4996d9d
commit f8ac21d891
7 changed files with 20 additions and 15 deletions

View file

@ -130,7 +130,7 @@ Zotero.API = {
// Filter results by item key
if (params.itemKey) {
ids = ids.filter(function (id) {
var [libraryID, key] = Zotero.Items.getLibraryAndKeyFromID(id);
var {libraryID, key} = Zotero.Items.getLibraryAndKeyFromID(id);
return params.itemKey.indexOf(key) !== -1;
});
}

View file

@ -50,7 +50,8 @@ Zotero.Attachments = new function(){
// Create a new attachment
var attachmentItem = new Zotero.Item('attachment');
if (parentItemID) {
let [parentLibraryID, parentKey] = Zotero.Items.getLibraryAndKeyFromID(parentItemID);
let {libraryID: parentLibraryID, key: parentKey} =
Zotero.Items.getLibraryAndKeyFromID(parentItemID);
attachmentItem.libraryID = parentLibraryID;
}
else if (libraryID) {
@ -151,7 +152,7 @@ Zotero.Attachments = new function(){
yield Zotero.DB.executeTransaction(function* () {
// Create a new attachment
var attachmentItem = new Zotero.Item('attachment');
let [libraryID, parentKey] = Zotero.Items.getLibraryAndKeyFromID(parentItemID);
let {libraryID, key: parentKey} = Zotero.Items.getLibraryAndKeyFromID(parentItemID);
attachmentItem.libraryID = libraryID;
attachmentItem.setField('title', title);
attachmentItem.setField('url', url);
@ -318,7 +319,8 @@ Zotero.Attachments = new function(){
attachmentItem.libraryID = libraryID;
}
else if (parentItemID) {
let [parentLibraryID, parentKey] = Zotero.Items.getLibraryAndKeyFromID(parentItemID);
let {libraryID: parentLibraryID, key: parentKey} =
Zotero.Items.getLibraryAndKeyFromID(parentItemID);
attachmentItem.libraryID = parentLibraryID;
}
attachmentItem.setField('title', title ? title : fileName);
@ -627,7 +629,8 @@ Zotero.Attachments = new function(){
attachmentItem.libraryID = libraryID;
}
else if (parentItemID) {
let [parentLibraryID, parentKey] = Zotero.Items.getLibraryAndKeyFromID(parentItemID);
let {libraryID: parentLibraryID, key: parentKey} =
Zotero.Items.getLibraryAndKeyFromID(parentItemID);
Zotero.debug('==-=');
Zotero.debug(parentItemID);
Zotero.debug(parentLibraryID);
@ -862,7 +865,7 @@ Zotero.Attachments = new function(){
if (!itemID) {
throw new Error("itemID not provided");
}
var [libraryID, key] = Zotero.Items.getLibraryAndKeyFromID(itemID);
var {libraryID, key} = Zotero.Items.getLibraryAndKeyFromID(itemID);
if (!key) {
throw new Error("Item " + itemID + " not found");
}
@ -1287,7 +1290,8 @@ Zotero.Attachments = new function(){
return Zotero.DB.executeTransaction(function* () {
var attachmentItem = new Zotero.Item('attachment');
if (parentItemID) {
let [parentLibraryID, parentKey] = Zotero.Items.getLibraryAndKeyFromID(parentItemID);
let {libraryID: parentLibraryID, key: parentKey} =
Zotero.Items.getLibraryAndKeyFromID(parentItemID);
if (parentLibraryID && linkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE) {
throw new Error("Cannot save linked file in non-local library");
}

View file

@ -161,7 +161,7 @@ Zotero.DataObject.prototype._getParentID = function () {
Zotero.DataObject.prototype._setParentID = function (id) {
return this._setParentKey(
id
? this.ObjectsClass.getLibraryAndKeyFromID(Zotero.DataObjectUtilities.checkDataID(id))[1]
? this.ObjectsClass.getLibraryAndKeyFromID(Zotero.DataObjectUtilities.checkDataID(id)).key
: null
);
}

View file

@ -270,10 +270,11 @@ Zotero.DataObjects.prototype.exists = function (itemID) {
/**
* @return {Array} Array with libraryID and key
* @return {Object} Object with 'libraryID' and 'key'
*/
Zotero.DataObjects.prototype.getLibraryAndKeyFromID = function (id) {
return this._objectKeys[id] ? this._objectKeys[id] : false;
var lk = this._objectKeys[id];
return lk ? { libraryID: lk[0], key: lk[1] } : false;
}
@ -413,7 +414,7 @@ Zotero.DataObjects.prototype.unload = function () {
var ids = Zotero.flattenArguments(arguments);
for (var i=0; i<ids.length; i++) {
let id = ids[i];
let [libraryID, key] = this.getLibraryAndKeyFromID(id);
let {libraryID, key} = this.getLibraryAndKeyFromID(id);
if (key) {
delete this._objectIDs[libraryID][key];
delete this._objectKeys[id];

View file

@ -4103,7 +4103,7 @@ Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options, patc
// Collections
yield this.loadCollections();
obj.collections = this.getCollections().map(function (id) {
return this.ContainerObjectsClass.getLibraryAndKeyFromID(id)[1];
return this.ContainerObjectsClass.getLibraryAndKeyFromID(id).key;
}.bind(this));
// Relations

View file

@ -347,7 +347,7 @@ Zotero.Search.prototype.addCondition = function (condition, operator, value, req
}
// Shortcut to add a collection (which must be loaded first)
else if (condition == 'collectionID') {
let [libraryID, key] = Zotero.Collections.getLibraryAndKeyFromID(value);
let {libraryID, key} = Zotero.Collections.getLibraryAndKeyFromID(value);
if (!key) {
let msg = "Collection " + value + " not found";
Zotero.debug(msg, 2);
@ -362,7 +362,7 @@ Zotero.Search.prototype.addCondition = function (condition, operator, value, req
}
// Shortcut to add a saved search (which must be loaded first)
else if (condition == 'savedSearchID') {
let [libraryID, key] = Zotero.Searches.getLibraryAndKeyFromID(value);
let {libraryID, key} = Zotero.Searches.getLibraryAndKeyFromID(value);
if (!key) {
let msg = "Saved search " + value + " not found";
Zotero.debug(msg, 2);

View file

@ -3126,7 +3126,7 @@ var ZoteroPane = new function()
var str = event.currentTarget.ownerDocument.popupNode.ownerDocument.defaultView.getSelection().toString();
var uri = event.currentTarget.ownerDocument.popupNode.ownerDocument.location.href;
var itemID = ZoteroPane.addItemFromPage();
var [libraryID, key] = Zotero.Items.getLibraryAndKeyFromID(itemID);
var {libraryID, key} = Zotero.Items.getLibraryAndKeyFromID(itemID);
ZoteroPane.newNote(false, key, str, uri)
};