Make Zotero.DataObject#fromJSON() synchronous

When called on an identified object (i.e., one with an id or
library/key), loadAllData() must be called first. When called on a new
object (which is more common anyway), fromJSON() can be called
immediately.
This commit is contained in:
Dan Stillman 2015-10-19 15:37:13 -04:00
parent 5d8670db1d
commit 7d8a1b2573
4 changed files with 25 additions and 18 deletions

View file

@ -676,9 +676,12 @@ Zotero.Collection.prototype.serialize = function(nested) {
}
Zotero.Collection.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) {
yield this.loadAllData();
/**
* Populate the object's data from an API JSON data object
*
* If this object is identified (has an id or library/key), loadAllData() must have been called.
*/
Zotero.Collection.prototype.fromJSON = function (json) {
if (!json.name) {
throw new Error("'name' property not provided for collection");
}
@ -687,7 +690,7 @@ Zotero.Collection.prototype.fromJSON = Zotero.Promise.coroutine(function* (json)
// TODO
//this.setRelations(json.relations);
});
}
Zotero.Collection.prototype.toResponseJSON = Zotero.Promise.coroutine(function* (options = {}) {

View file

@ -3708,11 +3708,12 @@ Zotero.Item.prototype.isCollection = function() {
}
Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) {
if (this._identified) {
yield this.loadAllData();
}
/**
* Populate the object's data from an API JSON data object
*
* If this object is identified (has an id or library/key), loadAllData() must have been called.
*/
Zotero.Item.prototype.fromJSON = function (json) {
if (!json.itemType && !this._itemTypeID) {
throw new Error("itemType property not provided");
}
@ -3843,7 +3844,7 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) {
let note = json.note;
this.setNote(note !== undefined ? note : "");
}
});
}
/**

View file

@ -819,9 +819,12 @@ Zotero.Search.prototype.search = Zotero.Promise.coroutine(function* (asTempTable
});
Zotero.Search.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) {
yield this.loadAllData();
/**
* Populate the object's data from an API JSON data object
*
* If this object is identified (has an id or library/key), loadAllData() must have been called.
*/
Zotero.Search.prototype.fromJSON = function (json) {
if (!json.name) {
throw new Error("'name' property not provided for search");
}
@ -835,7 +838,7 @@ Zotero.Search.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) {
condition.value
);
}
});
}
Zotero.Collection.prototype.toResponseJSON = Zotero.Promise.coroutine(function* (options = {}) {
var json = yield this.constructor._super.prototype.toResponseJSON.apply(this, options);

View file

@ -954,7 +954,7 @@ describe("Zotero.Item", function () {
dateModified: "2015-06-07T20:58:00Z",
};
var item = new Zotero.Item;
yield item.fromJSON(json);
item.fromJSON(json);
assert.equal(item.getField('accessDate'), '2015-06-07 20:56:00');
assert.equal(item.dateAdded, '2015-06-07 20:57:00');
assert.equal(item.dateModified, '2015-06-07 20:58:00');
@ -968,7 +968,7 @@ describe("Zotero.Item", function () {
dateModified: "2015-06-07 20:58:00",
};
var item = new Zotero.Item;
yield item.fromJSON(json);
item.fromJSON(json);
assert.strictEqual(item.getField('accessDate'), '');
// DEBUG: Should these be null, or empty string like other fields from getField()?
assert.isNull(item.dateAdded);
@ -992,14 +992,14 @@ describe("Zotero.Item", function () {
};
var item = new Zotero.Item;
yield item.fromJSON(json);
item.fromJSON(json);
var id = yield item.saveTx();
assert.sameDeepMembers(item.getCreatorsJSON(), json.creators);
})
it("should map a base field to an item-specific field", function* () {
var item = new Zotero.Item("bookSection");
yield item.fromJSON({
item.fromJSON({
"itemType":"bookSection",
"publicationTitle":"Publication Title"
});