www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit 285995807d084f335152457a69a232572e3595fd
parent afe0412c58a3156f3a9df46445cef390b96f4abd
Author: Dan Stillman <dstillman@zotero.org>
Date:   Fri,  1 May 2015 03:51:38 -0400

fromJSON() methods for data objects

Tests needed

Diffstat:
Mchrome/content/zotero/xpcom/data/collection.js | 14++++++++++++++
Mchrome/content/zotero/xpcom/data/item.js | 76+++++++++++++++++++++++++++++++++++++++++-----------------------------------
Mchrome/content/zotero/xpcom/search.js | 19+++++++++++++++++++
3 files changed, 74 insertions(+), 35 deletions(-)

diff --git a/chrome/content/zotero/xpcom/data/collection.js b/chrome/content/zotero/xpcom/data/collection.js @@ -692,6 +692,20 @@ Zotero.Collection.prototype.serialize = function(nested) { } +Zotero.Collection.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) { + yield this.loadAllData(); + + if (!json.name) { + throw new Error("'name' property not provided for collection"); + } + this.name = json.name; + this.parentKey = json.parentCollection ? json.parentCollection : false; + + // TODO + //this.setRelations(json.relations); +}); + + Zotero.Collection.prototype.toJSON = function (options, patch) { var obj = {}; if (options && options.includeKey) { diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js @@ -3988,26 +3988,44 @@ Zotero.Item.prototype.isCollection = function() { } -Zotero.Item.prototype.fromJSON = function (json) { - if (json.itemKey) this.key = json.itemKey; - if (json.itemType) this.setType(Zotero.ItemTypes.getID(json.itemType)); +Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) { + yield this.loadAllData(); - var changedFields = {}; + this.setType(Zotero.ItemTypes.getID(json.itemType)); + + var isValidForType = {}; + var setFields = {}; // Primary data for (var field in json) { let val = json[field]; switch (field) { - case 'itemKey': + case 'key': + case 'version': case 'itemType': case 'creators': - case 'deleted': + case 'note': + // Use? + case 'md5': + case 'mtime': break; case 'dateAdded': case 'dateModified': - this['_'+field] = val; + this['_' + field] = Zotero.Date.dateToSQL(Zotero.Date.isoToDate(val), true); + break; + + case 'parentItem': + this.parentKey = val; + break; + + case 'deleted': + this.deleted = !!val; + break; + + case 'creators': + this.setCreators(json.creators); break; case 'tags': @@ -4037,49 +4055,37 @@ Zotero.Item.prototype.fromJSON = function (json) { this.attachmentCharset = val; break; + case 'filename': + this.attachmentFilename = val; + break; + case 'path': this.attachmentPath = val; break; // Item fields default: - let changed = this.setField(field, json[field]); - if (changed) { - changedFields[field] = true; + isValidForType[field] = Zotero.ItemFields.isValidForType( + Zotero.ItemFields.getID(field), this.itemTypeID + ); + if (!isValidForType[field]) { + Zotero.logError("Discarding unknown JSON field " + field); + continue; } + this.setField(field, json[field]); + setFields[field] = true; } } // Clear existing fields not specified var previousFields = this.getUsedFields(true); - for each(let field in previousFields) { - if (!changedFields[field] && - // Invalid fields will already have been cleared by the type change - Zotero.ItemFields.isValidForType( - Zotero.ItemFields.getID(field), this.itemTypeID - ) - ) { + for (let field of previousFields) { + // Invalid fields will already have been cleared by the type change + if (!setFields[field] && isValidForType[field]) { this.setField(field, false); } } - // Deleted item flag - this.deleted = !!json.deleted; - - // Creators - let pos = 0; - if (json.creators) { - while (pos<json.creators.length) { - this.setCreator(pos, json.creators[pos]); - pos++; - } - } - // Remove item's remaining creators not in JSON - while (pos < this.numCreators()) { - // Keep removing last creator - this.removeCreator(this.numCreators() - 1); - } - // Both notes and attachments might have parents and notes if (this.isNote() || this.isAttachment()) { let parentKey = json.parentItem; @@ -4088,7 +4094,7 @@ Zotero.Item.prototype.fromJSON = function (json) { let note = json.note; this.setNote(note !== undefined ? note : ""); } -} +}); /** diff --git a/chrome/content/zotero/xpcom/search.js b/chrome/content/zotero/xpcom/search.js @@ -852,6 +852,25 @@ Zotero.Search.prototype.serialize = function() { } +Zotero.Search.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) { + yield this.loadAllData(); + + if (!json.name) { + throw new Error("'name' property not provided for search"); + } + this.name = json.name; + + for (let i = 0; i < json.conditions.length; i++) { + let condition = json.conditions[i]; + this.addCondition( + condition.condition, + condition.operator, + condition.value + ); + } +}); + + /* * Get the SQL string for the search */