From 24022623a12c1fed45059c0e00c94975067b2ca5 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 12 May 2015 20:02:45 -0400 Subject: [PATCH] Move patchBase into options in Zotero.Item.prototype.toJSON() Also: - Make .mode == 'patch' optional if .patchBase is provided. - Remove requirement for item to be unchanged, which hopefully wasn't there for a good reason - Add a few tests, though more are needed --- chrome/content/zotero/xpcom/data/item.js | 27 ++++++------ test/tests/itemTest.js | 54 ++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 12 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 026ba0c189..2b8c4a2161 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -4138,12 +4138,9 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) { /** * @param {Object} options - * @param {Object} patchBase */ -Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options, patchBase) { - if (this.hasChanged()) { - throw new Error("Cannot generate JSON from changed item"); - } +Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options) { + options = options || {}; if (options) { var mode = options.mode; @@ -4153,12 +4150,18 @@ Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options, patc } if (mode == 'patch') { - if (!patchBase) { + if (!options.patchBase) { throw new Error("Cannot use patch mode if patchBase not provided"); } } - else if (patchBase) { - Zotero.debug("Zotero.Item.toJSON: ignoring provided patchBase in " + mode + " mode", 2); + else if (options.patchBase) { + if (options.mode) { + Zotero.debug("Zotero.Item.toJSON: ignoring provided patchBase in " + mode + " mode", 2); + } + // If patchBase provided and no explicit mode, use 'patch' + else { + mode = 'patch'; + } } var obj = {}; @@ -4254,7 +4257,7 @@ Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options, patc obj.dateModified = Zotero.Date.sqlToISO8601(this.dateModified); if (mode == 'patch') { - for (let i in patchBase) { + for (let i in options.patchBase) { switch (i) { case 'key': case 'version': @@ -4263,7 +4266,7 @@ Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options, patc } if (i in obj) { - if (obj[i] === patchBase[i]) { + if (obj[i] === options.patchBase[i]) { delete obj[i]; } } @@ -4277,12 +4280,12 @@ Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options, patc }); -Zotero.Item.prototype.toResponseJSON = Zotero.Promise.coroutine(function* (options, patchBase) { +Zotero.Item.prototype.toResponseJSON = Zotero.Promise.coroutine(function* (options) { var json = { key: this.key, version: this.version, meta: {}, - data: yield this.toJSON(options, patchBase) + data: yield this.toJSON(options) }; // TODO: library block? diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index c183e30daf..089632d934 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -420,4 +420,58 @@ describe("Zotero.Item", function () { assert.sameDeepMembers(item.getTags(tags), tags.slice(0)); }) }) + + describe("#toJSON()", function () { + it("should output only fields with values in default mode", function* () { + var itemType = "book"; + var title = "Test"; + + var item = new Zotero.Item(itemType); + item.setField("title", title); + var id = yield item.save(); + item = yield Zotero.Items.getAsync(id); + var json = yield item.toJSON(); + + assert.equal(json.itemType, itemType); + assert.equal(json.title, title); + assert.isUndefined(json.date); + assert.isUndefined(json.numPages); + }) + + it("should output all fields in 'full' mode", function* () { + var itemType = "book"; + var title = "Test"; + + var item = new Zotero.Item(itemType); + item.setField("title", title); + var id = yield item.save(); + item = yield Zotero.Items.getAsync(id); + var json = yield item.toJSON({ mode: 'full' }); + assert.equal(json.title, title); + assert.equal(json.date, ""); + assert.equal(json.numPages, ""); + }) + + it("should output only fields that differ in 'patch' mode", function* () { + var itemType = "book"; + var title = "Test"; + var date = "2015-05-12"; + + var item = new Zotero.Item(itemType); + item.setField("title", title); + var id = yield item.save(); + item = yield Zotero.Items.getAsync(id); + var patchBase = yield item.toJSON(); + + item.setField("date", date); + yield item.save(); + var json = yield item.toJSON({ + patchBase: patchBase + }) + assert.isUndefined(json.itemType); + assert.isUndefined(json.title); + assert.equal(json.date, date); + assert.isUndefined(json.numPages); + }) + }) });