commit 14341ca16ce201405a9f8739757910ed9c2a153c
parent 79748b91320eea7cfd755b47078387519ab018fa
Author: Dan Stillman <dstillman@zotero.org>
Date: Thu, 7 Apr 2016 19:32:34 -0400
Clear item creators when calling setCreators() with an empty array
Diffstat:
2 files changed, 23 insertions(+), 0 deletions(-)
diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js
@@ -1078,6 +1078,14 @@ Zotero.Item.prototype.setCreator = function (orderIndex, data) {
* @param {Object[]} data - An array of creator data in internal or API JSON format
*/
Zotero.Item.prototype.setCreators = function (data) {
+ // If empty array, clear all existing creators
+ if (!data.length) {
+ while (this.hasCreatorAt(0)) {
+ this.removeCreator(0);
+ }
+ return;
+ }
+
for (let i = 0; i < data.length; i++) {
this.setCreator(i, data[i]);
}
diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js
@@ -435,6 +435,21 @@ describe("Zotero.Item", function () {
item = Zotero.Items.get(id);
assert.sameDeepMembers(item.getCreators(), creators);
})
+
+ it("should clear creators if empty array passed", function () {
+ var item = createUnsavedDataObject('item');
+ item.setCreators([
+ {
+ firstName: "First",
+ lastName: "Last",
+ fieldMode: 0,
+ creatorTypeID: 1
+ }
+ ]);
+ assert.lengthOf(item.getCreators(), 1);
+ item.setCreators([]);
+ assert.lengthOf(item.getCreators(), 0);
+ });
})
describe("#getAttachments()", function () {