You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2019/03/07 09:05:36 UTC

[sling-org-apache-sling-servlets-post] branch master updated: SLING-8278 provide non render blocking version of Sling.getSessionInfo and Sling.httpGet

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-post.git


The following commit(s) were added to refs/heads/master by this push:
     new 5addcf4  SLING-8278 provide non render blocking version of Sling.getSessionInfo and Sling.httpGet
5addcf4 is described below

commit 5addcf42d70906490e7460ac4aeca37f81004a2e
Author: Ruben Reusser <ru...@headwire.com>
AuthorDate: Tue Feb 19 06:17:58 2019 -0800

    SLING-8278 provide non render blocking version of Sling.getSessionInfo and Sling.httpGet
    
    Closes #4
---
 src/main/resources/system/sling.js | 39 +++++++++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 7 deletions(-)

diff --git a/src/main/resources/system/sling.js b/src/main/resources/system/sling.js
index c96587c..565e8ce 100644
--- a/src/main/resources/system/sling.js
+++ b/src/main/resources/system/sling.js
@@ -61,15 +61,23 @@ var Sling = null;
     /**
      * HTTP GET XHR Helper
      * @param {String} url The URL
+     * @param {Function} optional second parameter for async version of the method.
+     *        The callback will get the XHR object, method returns immediately
      * @return the XHR object, use .responseText for the data
      * @type String
      */
-    Sling.httpGet = function(url) {
+    Sling.httpGet = function(url, callback) {
         var httpcon = Sling.getXHR();
         if (httpcon) {
-            httpcon.open('GET', url, false);
-            httpcon.send(null);
-            return httpcon;
+            if(callback) {
+                httpcon.onload = function() { callback(this); };
+                httpcon.open('GET', url);
+                httpcon.send(null);
+            } else {
+                httpcon.open('GET', url, false);
+                httpcon.send(null);
+                return httpcon;
+            }
         } else {
             return null;
         }
@@ -83,7 +91,7 @@ var Sling = null;
      * @type String
      */
     Sling.dumpObj = function(obj, level) {
-        var res="";
+        var res = "";
         for (var a in obj) {
             if (typeof(obj[a])!="object") {
                 res+=a+":"+obj[a]+"  ";
@@ -104,7 +112,7 @@ var Sling = null;
      * @type Array
      */
     Sling.getAllPropNames = function(obj, names) {
-        var root=false;
+        var root = false;
         if (!names) {
             names=new Object();
             root=true;
@@ -209,7 +217,24 @@ var Sling = null;
         }
         return null;
     }
-    
+
+    /** Get "session info" from repository. Mainly answers the question: "Who am I"
+     *  and "Which workspace am I logged into. Async version of getSessionInfo
+     * @param {Function} callback function, will get an An Object tree as first argument
+     *        containing the session information, null if server status <> 200
+     */
+    Sling.getSessionInfoAsync = function(callback) {
+        var res = Sling.httpGet(Sling.baseurl+"/system/sling/info.sessionInfo.json",
+            function(res) {
+                if(res.status == 200) {
+                    var info = Sling.evalString(res.responseText);
+                    callback(info);
+                } else {
+                    callback(null);
+                }
+            });
+    }
+
     /** Replace extension in a path */
     Sling._replaceExtension = function(path,newExtension) {
         var i = path.lastIndexOf(".");