[open-ils-commits] r10854 - branches/acq-experiment/Open-ILS/web/js/dojo/openils

svn at svn.open-ils.org svn at svn.open-ils.org
Thu Oct 16 17:54:43 EDT 2008


Author: erickson
Date: 2008-10-16 17:54:38 -0400 (Thu, 16 Oct 2008)
New Revision: 10854

Added:
   branches/acq-experiment/Open-ILS/web/js/dojo/openils/Util.js
Log:
somewhere to put general stuff that has no better place to live.  added onload wrapper to prevent calls when there is no valid authtoken.  added method to extract responses and check for events

Added: branches/acq-experiment/Open-ILS/web/js/dojo/openils/Util.js
===================================================================
--- branches/acq-experiment/Open-ILS/web/js/dojo/openils/Util.js	                        (rev 0)
+++ branches/acq-experiment/Open-ILS/web/js/dojo/openils/Util.js	2008-10-16 21:54:38 UTC (rev 10854)
@@ -0,0 +1,82 @@
+/* ---------------------------------------------------------------------------
+ * Copyright (C) 2008  Georgia Public Library Service
+ * Bill Erickson <erickson at esilibrary.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * ---------------------------------------------------------------------------
+ */
+
+
+/**
+ * General purpose, static utility functions
+ */
+
+if(!dojo._hasResource["openils.Util"]) {
+    dojo._hasResource["openils.Util"] = true;
+    dojo.provide("openils.Util");
+    dojo.require('openils.Event');
+    dojo.declare('openils.Util', null, {});
+
+
+    /**
+     * Wrapper for dojo.addOnLoad that verifies a valid login session is active
+     * before adding the function to the onload set
+     */
+    openils.Util.addOnLoad = function(func, noSes) {
+        if(func) {
+            if(!noSes) {
+                dojo.require('openils.User');
+                if(!openils.User.authtoken) 
+                    return;
+            }
+            console.log("adding onload " + func.name);
+            dojo.addOnLoad(func);
+        }
+    };
+
+    /**
+     * Returns true if the provided array contains the specified value
+     */
+    openils.Util.arrayContains = function(arr, val) {
+        for(var i = 0; arr && i < arr.length; i++) {
+            if(arr[i] == val)
+                return true;
+        }
+        return false;
+    };
+
+    /**
+     * Parses opensrf response objects to see if they contain 
+     * data and/or an ILS event.  This only calls request.recv()
+     * once, so in a streaming context, it's necessary to loop on
+     * this method. 
+     * @param r The OpenSRF Request object
+     * @param eventOK If true, any found events will be returned as responses.  
+     * If false, they will be treated as error conditions and their content will
+     * be alerted if openils.Util.alertEvent is set to true.  Also, if eventOk is
+     * false, the response content will be null when an event is encountered.
+     */
+    openils.Util.alertEvent = true;
+    openils.Util.extractResponse = function(r, eventOk) {
+        var msg = r.recv();
+        if(msg == null) return msg;
+        var val = msg.content();
+        if(e = openils.Event.parse(val)) {
+            if(eventOk) return e;
+            console.log(e.toString());
+            if(openils.Util.alertEvent)
+                alert(e);
+            return null;
+        }
+        return val;
+    };
+
+}



More information about the open-ils-commits mailing list