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
This commit is contained in:
Dan Stillman 2015-05-12 20:02:45 -04:00
parent 1578675ace
commit 24022623a1
2 changed files with 69 additions and 12 deletions

View file

@ -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?

View file

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