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:
parent
1578675ace
commit
24022623a1
|
@ -4138,12 +4138,9 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @param {Object} patchBase
|
|
||||||
*/
|
*/
|
||||||
Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options, patchBase) {
|
Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options) {
|
||||||
if (this.hasChanged()) {
|
options = options || {};
|
||||||
throw new Error("Cannot generate JSON from changed item");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options) {
|
if (options) {
|
||||||
var mode = options.mode;
|
var mode = options.mode;
|
||||||
|
@ -4153,13 +4150,19 @@ Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options, patc
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == 'patch') {
|
if (mode == 'patch') {
|
||||||
if (!patchBase) {
|
if (!options.patchBase) {
|
||||||
throw new Error("Cannot use patch mode if patchBase not provided");
|
throw new Error("Cannot use patch mode if patchBase not provided");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (patchBase) {
|
else if (options.patchBase) {
|
||||||
|
if (options.mode) {
|
||||||
Zotero.debug("Zotero.Item.toJSON: ignoring provided patchBase in " + mode + " mode", 2);
|
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 = {};
|
var obj = {};
|
||||||
obj.key = this.key;
|
obj.key = this.key;
|
||||||
|
@ -4254,7 +4257,7 @@ Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options, patc
|
||||||
obj.dateModified = Zotero.Date.sqlToISO8601(this.dateModified);
|
obj.dateModified = Zotero.Date.sqlToISO8601(this.dateModified);
|
||||||
|
|
||||||
if (mode == 'patch') {
|
if (mode == 'patch') {
|
||||||
for (let i in patchBase) {
|
for (let i in options.patchBase) {
|
||||||
switch (i) {
|
switch (i) {
|
||||||
case 'key':
|
case 'key':
|
||||||
case 'version':
|
case 'version':
|
||||||
|
@ -4263,7 +4266,7 @@ Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options, patc
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i in obj) {
|
if (i in obj) {
|
||||||
if (obj[i] === patchBase[i]) {
|
if (obj[i] === options.patchBase[i]) {
|
||||||
delete obj[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 = {
|
var json = {
|
||||||
key: this.key,
|
key: this.key,
|
||||||
version: this.version,
|
version: this.version,
|
||||||
meta: {},
|
meta: {},
|
||||||
data: yield this.toJSON(options, patchBase)
|
data: yield this.toJSON(options)
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: library block?
|
// TODO: library block?
|
||||||
|
|
|
@ -420,4 +420,58 @@ describe("Zotero.Item", function () {
|
||||||
assert.sameDeepMembers(item.getTags(tags), tags.slice(0));
|
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);
|
||||||
|
})
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user