javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java
branchmodel
changeset 954 6448c284fe21
parent 949 3bd43aa6f08d
child 963 62d77cc38117
     1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java	Mon Apr 08 12:12:42 2013 +0200
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java	Mon Apr 08 16:51:30 2013 +0200
     1.3 @@ -73,7 +73,32 @@
     1.4      private static Object getProperty(Object object, String property) {
     1.5          return null;
     1.6      }
     1.7 +    
     1.8 +    public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
     1.9 +        int h = whenDone.hashCode();
    1.10 +        String name;
    1.11 +        for (;;) {
    1.12 +            name = "jsonp" + Integer.toHexString(h);
    1.13 +            if (defineIfUnused(name, jsonResult, whenDone)) {
    1.14 +                return name;
    1.15 +            }
    1.16 +            h++;
    1.17 +        }
    1.18 +    }
    1.19  
    1.20 +    @JavaScriptBody(args = { "name", "arr", "run" }, body = 
    1.21 +        "if (window[name]) return false;\n "
    1.22 +      + "window[name] = function(data) {\n "
    1.23 +      + "  arr[0] = data;\n"
    1.24 +      + "  run.run__V();\n"
    1.25 +      + "  delete window[name];\n"
    1.26 +      + "};"
    1.27 +      + "return true;\n"
    1.28 +    )
    1.29 +    private static boolean defineIfUnused(String name, Object[] arr, Runnable run) {
    1.30 +        return true;
    1.31 +    }
    1.32 +    
    1.33      @JavaScriptBody(args = { "url", "arr", "callback" }, body = ""
    1.34          + "var request = new XMLHttpRequest();\n"
    1.35          + "request.open('GET', url, true);\n"
    1.36 @@ -89,11 +114,33 @@
    1.37          + "};"
    1.38          + "request.send();"
    1.39      )
    1.40 -    public static void loadJSON(
    1.41 +    private static void loadJSON(
    1.42          String url, Object[] jsonResult, Runnable whenDone
    1.43      ) {
    1.44      }
    1.45      
    1.46 +    public static void loadJSON(
    1.47 +        String url, Object[] jsonResult, Runnable whenDone, String jsonp
    1.48 +    ) {
    1.49 +        if (jsonp == null) {
    1.50 +            loadJSON(url, jsonResult, whenDone);
    1.51 +        } else {
    1.52 +            loadJSONP(url, jsonp);
    1.53 +        }
    1.54 +    }
    1.55 +    
    1.56 +    @JavaScriptBody(args = { "url", "jsonp" }, body = 
    1.57 +        "var scrpt = window.document.createElement('script');\n "
    1.58 +        + "scrpt.setAttribute('src', url);\n "
    1.59 +        + "scrpt.setAttribute('id', jsonp);\n "
    1.60 +        + "scrpt.setAttribute('type', 'text/javascript');\n "
    1.61 +        + "var body = document.getElementsByTagName('body')[0];\n "
    1.62 +        + "body.appendChild(scrpt);\n"
    1.63 +    )
    1.64 +    private static void loadJSONP(String url, String jsonp) {
    1.65 +        
    1.66 +    }
    1.67 +    
    1.68      public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
    1.69          for (int i = 0; i < props.length; i++) {
    1.70              values[i] = getProperty(jsonObject, props[i]);