Refactor can_have_copies check in copy_broswer.js.
authorJason Stephenson <jstephenson@mvlc.org>
Tue, 4 Oct 2011 15:00:21 +0000 (11:00 -0400)
committerJason Stephenson <jstephenson@mvlc.org>
Tue, 15 Nov 2011 22:14:25 +0000 (17:14 -0500)
Since we want to check in two places, move the can_have_copies flag
and source name to be variable members of the this object.

Initialize the members in the source_init function.

Check the members in the add_volumes and add_items commands.

Signed-off-by: Jason Stephenson <jstephenson@mvlc.org>
Open-ILS/xul/staff_client/server/cat/copy_browser.js

index 69cae98..af4d653 100644 (file)
@@ -34,6 +34,8 @@ cat.copy_browser.prototype = {
 
             obj.list_init(params);
 
+                       obj.source_init();
+
             obj.controller.render();
 
             obj.default_depth = obj.depth_menu_init();
@@ -339,6 +341,11 @@ cat.copy_browser.prototype = {
 
                                     if (edit==0) return; // no read-only view for this interface
 
+                                    if (!obj.can_have_copies) {
+                                        alert(document.getElementById('catStrings').getFormattedString('staff.cat.copy_browser.can_have_copies.false', obj.source));
+                                        return;
+                                    }
+
                                     var title = document.getElementById('catStrings').getString('staff.cat.copy_browser.add_item.title');
 
                                     var url;
@@ -633,31 +640,11 @@ cat.copy_browser.prototype = {
                                         return; // no read-only view for this interface
                                     }
 
-                                    var can_have_copies = true;
-                                    var cbsObj;
-                                    try {
-                                        var bibObj = obj.network.request(
-                                            api.FM_BRE_RETRIEVE_VIA_ID.app,
-                                            api.FM_BRE_RETRIEVE_VIA_ID.method,
-                                            [ ses(), [obj.docid] ]
-                                        );
-                                        bibObj = bibObj[0];
-                                        cbsObj = obj.network.request(
-                                            api.FM_CBS_RETRIEVE_VIA_PCRUD.app,
-                                            api.FM_CBS_RETRIEVE_VIA_PCRUD.method,
-                                            [ ses(), bibObj.source() ]
-                                        );
-                                        can_have_copies = (cbsObj.can_have_copies() == get_db_true());
-                                    } catch(E) {
-                                        obj.error.sdump('D_ERROR','can have copies check: ' + E);
+                                    if (!obj.can_have_copies) {
+                                        alert(document.getElementById('catStrings').getFormattedString('staff.cat.copy_browser.can_have_copies.false', obj.source));
                                         return;
                                     }
 
-                                    if (!can_have_copies) {
-                                        alert(document.getElementById('catStrings').getFormattedString('staff.cat.copy_browser.can_have_copies.false', [cbsObj.source()]));
-                                        return false;
-                                    }
-
                                     var title = document.getElementById('catStrings').getString('staff.cat.copy_browser.add_volume.title');
 
                                     var url;
@@ -1864,6 +1851,30 @@ cat.copy_browser.prototype = {
         }
     },
 
+    // Sets can_have_copies and source member variables.
+    'source_init' : function() {
+        var obj = this;
+        var cbsObj;
+               try {
+                       var bibObj = obj.network.request(
+                api.FM_BRE_RETRIEVE_VIA_ID.app,
+                api.FM_BRE_RETRIEVE_VIA_ID.method,
+                [ ses(), [obj.docid] ]
+            );
+                       bibObj = bibObj[0];
+                       cbsObj = obj.network.request(
+                api.FM_CBS_RETRIEVE_VIA_PCRUD.app,
+                api.FM_CBS_RETRIEVE_VIA_PCRUD.method,
+                [ ses(), bibObj.source() ]
+            );
+                       obj.can_have_copies = (cbsObj.can_have_copies() == get_db_true());
+            obj.source = cbsObj.source();
+               } catch(E) {
+                       obj.error.sdump('D_ERROR','can have copies check: ' + E);
+                       alert(E);
+               }
+       },
+
     'toggle_actions' : function() {
         var obj = this;
         try {