make Searcher.js load it's store data asynchronously. This was prompted by IE occais... async_fielder
authorBill Erickson <erickson@esilibrary.com>
Tue, 14 Dec 2010 21:55:12 +0000 (16:55 -0500)
committerBill Erickson <erickson@esilibrary.com>
Tue, 14 Dec 2010 21:55:12 +0000 (16:55 -0500)
Open-ILS/web/js/dojo/openils/widget/Searcher.js

index 896c049..f839c5b 100644 (file)
@@ -22,7 +22,7 @@ if(!dojo._hasResource["openils.widget.Searcher"]) {
     dojo.require("fieldmapper.AutoIDL");
     dojo.require("openils.I18N");
     dojo.require("DojoSRF");
-    dojo.require("dojo.data.ItemFileReadStore");
+    dojo.require("dojo.data.ItemFileWriteStore");
     dojo.require("dijit._Widget");
     dojo.require("dijit._Templated");
     dojo.require("dijit.form.Button");
@@ -265,91 +265,131 @@ if(!dojo._hasResource["openils.widget.Searcher"]) {
 
     openils.widget.Searcher._cache = {arr : {}, obj : {}, store : {}};
 
+    function postProcess() {
+
+        var facet_list = [];
+        var search_list = [];
+
+        dojo.forEach(
+            openils.widget.Searcher._cache.arr.cmc,
+            function (cmc_obj) {
+                search_list.push({
+                    name    : cmc_obj.name,
+                    label   : cmc_obj.label
+                });
+                dojo.forEach(
+                    dojo.filter(
+                        openils.widget.Searcher._cache.arr.cmf,
+                        function (x) {
+                            if (x.field_class == cmc_obj.name) return 1;
+                            return 0;
+                        }
+                    ),
+                    function (cmf_obj) {
+                        if (cmf_obj.search_field == 't') {
+                            search_list.push({
+                                name : cmc_obj.name + '|' + cmf_obj.name,
+                                label : ' -- ' + cmf_obj.label
+                            });
+                        }
+                        if (cmf_obj.facet_field == 't') {
+                            facet_list.push({
+                                name : cmc_obj.name + '|' + cmf_obj.name,
+                                label : cmc_obj.label + ' : '  + cmf_obj.label
+                            });
+                        }
+                    }
+                )
+            }
+        );
+
+        dojo.forEach(facet_list, function(facet) { openils.widget.Searcher.facetStore.newItem(facet); });
+        dojo.forEach(search_list, function(search) { openils.widget.Searcher.searchStore.newItem(search); });
+    }
+
+    var totalReceived = 0;
+    function postProcessResult(c, fielder_result) {
+        totalReceived++;
+
+        var sorted_fielder_result = fielder_result.sort( function(a,b) {
+            if(a[c.label] > b[c.label]) return 1;
+            if(a[c.label] < b[c.label]) return -1;
+            return 0;
+        });
+
+        openils.widget.Searcher._cache.arr[c.classname] = sorted_fielder_result;
+
+        var list = [];
+        openils.widget.Searcher._cache.obj[c.classname] = {};
+
+        dojo.forEach(
+            openils.widget.Searcher._cache.arr[c.classname],
+            function (x) {
+                openils.widget.Searcher._cache.obj[c.classname][x[c.ident]] = x;
+                list.push(x);
+            }
+        );
+
+        dojo.forEach(list, function(item) { 
+            openils.widget.Searcher._cache.store[c.classname].newItem(item);
+        });
+
+        if(totalReceived == 2)
+            postProcess();
+    }
+
+    // seed the stores
+    openils.widget.Searcher.facetStore = 
+        new dojo.data.ItemFileWriteStore( { data : {identifier : 'name', label : 'label', items : [] } } );
+
+    openils.widget.Searcher.searchStore = 
+        new dojo.data.ItemFileWriteStore( { data : {identifier : 'name', label : 'label', items : [] } } );
+
     dojo.forEach(
-        [ {ident:'name',classname:'cmc',label:'label',fields:null,cookie:true}, {ident:'id',classname:'cmf',label:'label',fields:['id','field_class','name','search_field','facet_field','label']} ],
-        // [ {ident:'name',classname:'cmc',label:'label',fields:null}, {ident:'id',classname:'cmf',label:'label',fields:null}, {ident:'alias',classname:'cmsa',label:'alias',fields:null} ],
+        [ 
+            {ident:'name', classname:'cmc', label:'label', fields:null, cookie:true}, 
+            {ident:'id', classname:'cmf', label:'label', fields:['id','field_class','name','search_field','facet_field','label']} 
+        ],
         function (c) {
 
+            // seed the store
+            openils.widget.Searcher._cache.store[c.classname] = 
+                new dojo.data.ItemFileWriteStore( { data : {identifier : c.ident, label : c.label, items : [] } } );
+
             var fielder_result = c.cookie ? dojo.cookie('SRCHR' + c.classname) : null;
+
             if (fielder_result) {
                 fielder_result = dojo.fromJson(fielder_result);
+                postProcessResult(c, fielder_result);
+
             } else {
+
                 var q = {};
                 q[c.ident] = { '!=' :  null };
 
-                fielder_result = fieldmapper.standardRequest(
+                fieldmapper.standardRequest(
                     [ 'open-ils.fielder', 'open-ils.fielder.'+c.classname+'.atomic'],
-                    [ { cache : 1, query : q, fields: c.fields } ]
-                );
-                if (c.cookie) dojo.cookie(
-                    'SRCHR' + c.classname,
-                    dojo.toJson(fielder_result),
-                    { path : location.href.replace(/^https?:\/\/[^\/]+(\/.*\w{2}-\w{2}\/).*/, "$1") }
+                    {
+                        async : true,
+                        params : [ { cache : 1, query : q, fields: c.fields } ],
+                        oncomplete : function(r) {
+                            dojo.require('openils.Util');
+                            var fielder_result = openils.Util.readResponse(r);
+
+                            if (c.cookie) dojo.cookie(
+                                'SRCHR' + c.classname,
+                                dojo.toJson(fielder_result),
+                                { path : location.href.replace(/^https?:\/\/[^\/]+(\/.*\w{2}-\w{2}\/).*/, "$1") }
+                            );
+
+                            postProcessResult(c, fielder_result);
+                        }
+                    }
                 );
             }
-
-            var sorted_fielder_result = fielder_result.sort( function(a,b) {
-                if(a[c.label] > b[c.label]) return 1;
-                if(a[c.label] < b[c.label]) return -1;
-                return 0;
-            });
-
-            openils.widget.Searcher._cache.arr[c.classname] = sorted_fielder_result;
-
-            var list = [];
-            openils.widget.Searcher._cache.obj[c.classname] = {};
-
-            dojo.forEach(
-                openils.widget.Searcher._cache.arr[c.classname],
-                function (x) {
-                    openils.widget.Searcher._cache.obj[c.classname][x[c.ident]] = x;
-                    list.push(x);
-                }
-            );
-
-            openils.widget.Searcher._cache.store[c.classname] = new dojo.data.ItemFileReadStore( { data : {identifier : c.ident, label : c.label, items : list } } );
         }
     );
 
-    var facet_list = [];
-    var search_list = [];
-
-    dojo.forEach(
-        openils.widget.Searcher._cache.arr.cmc,
-        function (cmc_obj) {
-            search_list.push({
-                name    : cmc_obj.name,
-                label   : cmc_obj.label
-            });
-            dojo.forEach(
-                dojo.filter(
-                    openils.widget.Searcher._cache.arr.cmf,
-                    function (x) {
-                        if (x.field_class == cmc_obj.name) return 1;
-                        return 0;
-                    }
-                ),
-                function (cmf_obj) {
-                    if (cmf_obj.search_field == 't') {
-                        search_list.push({
-                            name : cmc_obj.name + '|' + cmf_obj.name,
-                            label : ' -- ' + cmf_obj.label
-                        });
-                    }
-                    if (cmf_obj.facet_field == 't') {
-                        facet_list.push({
-                            name : cmc_obj.name + '|' + cmf_obj.name,
-                            label : cmc_obj.label + ' : '  + cmf_obj.label
-                        });
-                    }
-                }
-            )
-        }
-    );
-
-    openils.widget.Searcher.facetStore = new dojo.data.ItemFileReadStore( { data : {identifier : 'name', label : 'label', items : facet_list} } );
-    openils.widget.Searcher.searchStore = new dojo.data.ItemFileReadStore( { data : {identifier : 'name', label : 'label', items : search_list} } );
-
 //------ modifiers template -----------------------------------------------
 
     dojo._hasResource["openils.widget.Searcher.modifier"] = true;