Various creator-saving fixes

This commit is contained in:
Dan Stillman 2015-05-08 13:26:11 -04:00
parent 9f8510c821
commit 5635fec4e3
3 changed files with 55 additions and 4 deletions

View file

@ -175,6 +175,7 @@ Zotero.Creators = new function() {
switch (field) {
case 'firstName':
case 'lastName':
if (val === undefined) continue;
cleanedData[field] = val.trim().normalize();
break;
@ -184,9 +185,9 @@ Zotero.Creators = new function() {
}
}
// Handle API JSON format
// Handle API JSON .name
if (data.name !== undefined) {
cleanedData.lastName = data.name.trim();
cleanedData.lastName = data.name.trim().normalize();
cleanedData.fieldMode = 1;
}

View file

@ -974,6 +974,11 @@ Zotero.Item.prototype.getCreatorsJSON = function () {
* </ul>
*/
Zotero.Item.prototype.setCreator = function (orderIndex, data) {
var itemTypeID = this._itemTypeID;
if (!itemTypeID) {
throw new Error('Item type must be set before setting creators');
}
this._requireData('creators');
data = Zotero.Creators.cleanData(data);
@ -983,7 +988,6 @@ Zotero.Item.prototype.setCreator = function (orderIndex, data) {
}
// If creatorTypeID isn't valid for this type, use the primary type
var itemTypeID = this._itemTypeID;
if (!data.creatorTypeID || !Zotero.CreatorTypes.isValidForItemType(data.creatorTypeID, itemTypeID)) {
var msg = "Creator type '" + Zotero.CreatorTypes.getName(data.creatorTypeID) + "' "
+ "isn't valid for " + Zotero.ItemTypes.getName(itemTypeID)
@ -4022,7 +4026,6 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) {
case 'key':
case 'version':
case 'itemType':
case 'creators':
case 'note':
// Use?
case 'md5':

View file

@ -242,6 +242,53 @@ describe("Zotero.Item", function () {
})
});
describe("#setCreators", function () {
it("should accept an array of creators in API JSON format", function* () {
var creators = [
{
firstName: "First",
lastName: "Last",
creatorType: "author"
},
{
name: "Test Name",
creatorType: "editor"
}
];
var item = new Zotero.Item("journalArticle");
item.setCreators(creators);
var id = yield item.save();
item = yield Zotero.Items.getAsync(id);
yield item.loadCreators();
assert.sameDeepMembers(item.getCreatorsJSON(), creators);
})
it("should accept an array of creators in internal format", function* () {
var creators = [
{
firstName: "First",
lastName: "Last",
fieldMode: 0,
creatorTypeID: 1
},
{
firstName: "",
lastName: "Test Name",
fieldMode: 1,
creatorTypeID: 2
}
];
var item = new Zotero.Item("journalArticle");
item.setCreators(creators);
var id = yield item.save();
item = yield Zotero.Items.getAsync(id);
yield item.loadCreators();
assert.sameDeepMembers(item.getCreators(), creators);
})
})
describe("#attachmentCharset", function () {
it("should get and set a value", function* () {
var charset = 'utf-8';