Clear item creators when calling setCreators() with an empty array

This commit is contained in:
Dan Stillman 2016-04-07 19:32:34 -04:00
parent 79748b9132
commit 14341ca16c
2 changed files with 23 additions and 0 deletions

View file

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

View file

@ -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 () {