commit 7d8a1b2573465b0a6a5a51c1aa917fd31595af08
parent 5d8670db1d4c20f0e833e30573a8d2bdcb3fdb4d
Author: Dan Stillman <dstillman@zotero.org>
Date: Mon, 19 Oct 2015 15:37:13 -0400
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.
Diffstat:
4 files changed, 25 insertions(+), 18 deletions(-)
diff --git a/chrome/content/zotero/xpcom/data/collection.js b/chrome/content/zotero/xpcom/data/collection.js
@@ -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 = {}) {
diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js
@@ -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 : "");
}
-});
+}
/**
diff --git a/chrome/content/zotero/xpcom/search.js b/chrome/content/zotero/xpcom/search.js
@@ -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);
diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js
@@ -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"
});