AutoFieldWidget single-object cache repairs
authorBill Erickson <berick@esilibrary.com>
Thu, 22 Mar 2012 17:41:29 +0000 (13:41 -0400)
committerMike Rylander <mrylander@gmail.com>
Fri, 23 Mar 2012 20:11:22 +0000 (16:11 -0400)
Caching single objects linked off the context object was failing in most
cases as it was caching under the display name instead of the true value
(pkey) for the linked object.  This fixes that by always caching the
linked object by its true value, so that future cache lookups will be
looking in the right place.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/web/js/dojo/openils/widget/AutoFieldWidget.js

index ad5d92b..50d1a4f 100644 (file)
@@ -389,14 +389,18 @@ if(!dojo._hasResource['openils.widget.AutoFieldWidget']) {
             }
 
             // then try the single object cache
-            if(this.cache[this.auth].single[lclass] && 
-                    this.cache[this.auth].single[lclass][this.widgetValue] &&
-                    this.cache[this.auth].single[lclass][this.widgetValue][self.labelFormat || '']) {
-                this.widgetValue = this.cache[this.auth].single[lclass][this.widgetValue][self.labelFormat || ''];
+            var item;
+            if(this.cache[this.auth].single[lclass] && (
+                item = this.cache[this.auth].single[lclass][this.widgetValue]) ) {
+
+                this.widgetValue = (this.labelFormat) ? 
+                    this._applyLabelFormat(item.toStoreItem(), this.labelFormat) :
+                    item[linkInfo.vfield.selector]();
+
                 return;
             }
 
-            console.log("Fetching sync object " + lclass + " : " + this.widgetValue);
+            console.log("Fetching linked object " + lclass + " : " + this.widgetValue);
 
             // if those fail, fetch the linked object
             this.async = true;
@@ -406,22 +410,14 @@ if(!dojo._hasResource['openils.widget.AutoFieldWidget']) {
                 oncomplete : function(r) {
                     var item = openils.Util.readResponse(r);
 
-                    var newvalue = item[linkInfo.vfield.selector]();
-
-                    var labelCacheKey = ''; 
-
-                    if(self.labelFormat) {
-                        labelCacheKey = self.labelFormat;
-                        self.widgetValue = self._applyLabelFormat(item.toStoreItem(), self.labelFormat);
-                    } else {
-                        self.widgetValue = newvalue;
-                    }
-
+                    // cache the true object under its real value
                     if(!self.cache[self.auth].single[lclass])
                         self.cache[self.auth].single[lclass] = {};
-                    if(!self.cache[self.auth].single[lclass][self.widgetValue])
-                        self.cache[self.auth].single[lclass][self.widgetValue] = {};
-                    self.cache[self.auth].single[lclass][self.widgetValue][labelCacheKey] = newvalue;
+                    self.cache[self.auth].single[lclass][self.widgetValue] = item;
+
+                    self.widgetValue = (self.labelFormat) ? 
+                        self._applyLabelFormat(item.toStoreItem(), self.labelFormat) :
+                        item[linkInfo.vfield.selector]();
 
                     self.widget.startup();
                     self._widgetLoaded();