From b83bc40426e11609ae5e3203df5134964971d652 Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Fri, 14 Nov 2014 04:35:26 -0600 Subject: [PATCH] Various fixes for Zotero.Item --- .../content/zotero/xpcom/data/collection.js | 2 +- chrome/content/zotero/xpcom/data/item.js | 54 +++++++++++-------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/collection.js b/chrome/content/zotero/xpcom/data/collection.js index a97a7937d..ae9007fb1 100644 --- a/chrome/content/zotero/xpcom/data/collection.js +++ b/chrome/content/zotero/xpcom/data/collection.js @@ -885,7 +885,7 @@ Zotero.Collection.prototype.loadChildCollections = Zotero.Promise.coroutine(func this._childCollections = []; - if (ids) { + if (ids.length) { for each(var id in ids) { var col = yield this.ObjectsClass.getAsync(id); if (!col) { diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 6fd8687e5..147452fb9 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -69,9 +69,9 @@ Zotero.Item = function(itemTypeOrID) { this._attachments = null; this._notes = null; - this._tags = {}; - this._collections = {}; - this._relations = {}; + this._tags = []; + this._collections = []; + this._relations = []; this._bestAttachmentState = null; this._fileExists = null; @@ -291,13 +291,13 @@ Zotero.Item.prototype.getField = function(field, unformatted, includeBaseMapped) * @param {Boolean} asNames * @return {Integer{}|String[]} */ -Zotero.Item.prototype.getUsedFields = Zotero.Promise.coroutine(function* (asNames) { +Zotero.Item.prototype.getUsedFields = function(asNames) { this._requireData('itemData'); return Object.keys(this._itemData) - .filter(id => this._itemData[id] !== false) + .filter(id => this._itemData[id] !== false && this._itemData[id] !== null) .map(id => asNames ? Zotero.ItemFields.getName(id) : parseInt(id)); -}); +}; @@ -1325,7 +1325,7 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) { continue; } - let previousCreatorID = this._previousData.creators[orderIndex] + let previousCreatorID = !isNew && this._previousData.creators[orderIndex] ? this._previousData.creators[orderIndex].id : false; let newCreatorID = yield Zotero.Creators.getIDFromData(creatorData, true); @@ -1603,7 +1603,7 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) { // Collections if (this._changed.collections) { - let oldCollections = this._previousData.collections; + let oldCollections = this._previousData.collections || []; let newCollections = this._collections; let toAdd = Zotero.Utilities.arrayDiff(newCollections, oldCollections); @@ -3766,6 +3766,7 @@ Zotero.Item.prototype.clone = function(libraryID, skipTags) { var sameLibrary = libraryID == this.libraryID; var newItem = new Zotero.Item; + newItem.libraryID = libraryID; newItem.setType(this.itemTypeID); var fieldIDs = this.getUsedFields(); @@ -3950,7 +3951,7 @@ Zotero.Item.prototype.fromJSON = function (json) { case 'dateAdded': case 'dateModified': - item[field] = val; + this['_'+field] = val; break; case 'tags': @@ -3999,8 +4000,9 @@ Zotero.Item.prototype.fromJSON = function (json) { if (!changedFields[field] && // Invalid fields will already have been cleared by the type change Zotero.ItemFields.isValidForType( - Zotero.ItemFields.getID(field), data.itemTypeID - )) { + Zotero.ItemFields.getID(field), this.itemTypeID + ) + ) { this.setField(field, false); } } @@ -4009,18 +4011,17 @@ Zotero.Item.prototype.fromJSON = function (json) { this.deleted = !!json.deleted; // Creators - var numCreators = 0; + let pos = 0; if (json.creators) { - for each (let creator in json.creators) { - this.setCreator(pos, creator); - numCreators++; + while (pos