1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java Thu Apr 18 20:09:45 2013 +0200
1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java Thu Apr 18 23:09:30 2013 +0200
1.3 @@ -40,28 +40,6 @@
1.4 static Knockout next;
1.5 private final Object model;
1.6
1.7 - static {
1.8 - BufferedReader r = new BufferedReader(new InputStreamReader(Knockout.class.getResourceAsStream("knockout-2.2.1.js")));
1.9 - StringBuilder sb = new StringBuilder();
1.10 - for (;;) {
1.11 - try {
1.12 - String l = r.readLine();
1.13 - if (l == null) {
1.14 - break;
1.15 - }
1.16 - sb.append(l).append('\n');
1.17 - } catch (IOException ex) {
1.18 - throw new IllegalStateException(ex);
1.19 - }
1.20 - }
1.21 - web().executeScript(sb.toString());
1.22 - Object ko = web().executeScript("ko");
1.23 - assert ko != null : "Knockout library successfully defined 'ko'";
1.24 -
1.25 - Console.register(web());
1.26 - }
1.27 -
1.28 -
1.29 Knockout(Object model) {
1.30 this.model = model == null ? this : model;
1.31 }
1.32 @@ -70,43 +48,15 @@
1.33 return model;
1.34 }
1.35
1.36 - private static final JSObject KObject;
1.37 - static {
1.38 - KObject = (JSObject) web().executeScript(
1.39 - "(function(scope) {"
1.40 - + " var kCnt = 0; "
1.41 - + " scope.KObject = {};"
1.42 - + " scope.KObject.create= function(value) {"
1.43 - + " var cnt = ++kCnt;"
1.44 - + " var ret = {};"
1.45 - + " ret.toString = function() { return 'KObject' + cnt + ' value: ' + value + ' props: ' + Object.keys(this); };"
1.46 - + " return ret;"
1.47 - + " };"
1.48 -
1.49 - + " scope.KObject.array= function() {"
1.50 - + " return Array.prototype.slice.call(arguments);"
1.51 - + " };"
1.52 -
1.53 - + " scope.KObject.expose = function(bindings, model, prop, sig) {"
1.54 - + " bindings[prop] = function(data, ev) {"
1.55 -// + " console.log(\" callback on prop: \" + prop);"
1.56 - + " model[sig](data, ev);"
1.57 - + " };"
1.58 - + " };"
1.59 -
1.60 - + "})(window); window.KObject"
1.61 - );
1.62 - }
1.63 -
1.64 static Object toArray(Object[] arr) {
1.65 - return KObject.call("array", arr);
1.66 + return InvokeJS.KObject.call("array", arr);
1.67 }
1.68
1.69 public static <M> Knockout applyBindings(
1.70 Object model, String[] propsGettersAndSetters,
1.71 String[] methodsAndSignatures
1.72 ) {
1.73 - Object bindings = KObject.call("create", model);
1.74 + Object bindings = InvokeJS.KObject.call("create", model);
1.75 applyImpl(propsGettersAndSetters, model.getClass(), bindings, model, methodsAndSignatures);
1.76 return new Knockout(bindings);
1.77 }
1.78 @@ -117,7 +67,7 @@
1.79 Object bindings = next;
1.80 next = null;
1.81 if (bindings == null) {
1.82 - bindings = KObject.call("create", model);
1.83 + bindings = InvokeJS.KObject.call("create", model);
1.84 }
1.85 applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures);
1.86 applyBindings(bindings);
1.87 @@ -167,6 +117,9 @@
1.88 Object bindings, Object model, String prop, String getter, String setter, boolean primitive, boolean array
1.89 ) {
1.90 WebEngine e = web();
1.91 + if (e == null) {
1.92 + return;
1.93 + }
1.94 JSObject bnd = (JSObject) e.executeScript("var x = {}; x.bnd = "
1.95 + "new Function('ko', 'bindings', 'model', 'prop', 'getter', 'setter', 'primitive', 'array', '"
1.96 + "var bnd = {"
1.97 @@ -198,7 +151,7 @@
1.98 + "};"
1.99 + "bindings[prop] = ko.computed(bnd);'"
1.100 + "); x;");
1.101 -
1.102 +
1.103 Object ko = e.executeScript("ko");
1.104 try {
1.105 KOProperty kop = new KOProperty(model, strip(getter), strip(setter));
1.106 @@ -224,9 +177,13 @@
1.107 private static void expose(
1.108 Object bindings, Object model, String prop, String sig
1.109 ) {
1.110 + WebEngine e = web();
1.111 + if (e == null) {
1.112 + return;
1.113 + }
1.114 try {
1.115 KOFunction f = new KOFunction(model, strip(sig));
1.116 - KObject.call("expose", bindings, f, prop, "call");
1.117 + InvokeJS.KObject.call("expose", bindings, f, prop, "call");
1.118 } catch (Throwable ex) {
1.119 LOG.log(Level.SEVERE, "Cannot define binding for " + prop + " in model " + model, ex);
1.120 }
1.121 @@ -234,8 +191,10 @@
1.122
1.123 @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
1.124 private static void applyBindings(Object bindings) {
1.125 - JSObject ko = (JSObject) web().executeScript("ko");
1.126 - ko.call("applyBindings", bindings);
1.127 + if (web() != null) {
1.128 + JSObject ko = (JSObject) web().executeScript("ko");
1.129 + ko.call("applyBindings", bindings);
1.130 + }
1.131 }
1.132
1.133 private static WebEngine web() {
1.134 @@ -267,4 +226,50 @@
1.135 bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]);
1.136 }
1.137 }
1.138 +
1.139 + private static final class InvokeJS {
1.140 + static final JSObject KObject;
1.141 +
1.142 + static {
1.143 + BufferedReader r = new BufferedReader(new InputStreamReader(Knockout.class.getResourceAsStream("knockout-2.2.1.js")));
1.144 + StringBuilder sb = new StringBuilder();
1.145 + for (;;) {
1.146 + try {
1.147 + String l = r.readLine();
1.148 + if (l == null) {
1.149 + break;
1.150 + }
1.151 + sb.append(l).append('\n');
1.152 + } catch (IOException ex) {
1.153 + throw new IllegalStateException(ex);
1.154 + }
1.155 + }
1.156 + web().executeScript(sb.toString());
1.157 + Object ko = web().executeScript("ko");
1.158 + assert ko != null : "Knockout library successfully defined 'ko'";
1.159 +
1.160 + Console.register(web());
1.161 + KObject = (JSObject) web().executeScript(
1.162 + "(function(scope) {"
1.163 + + " var kCnt = 0; "
1.164 + + " scope.KObject = {};"
1.165 + + " scope.KObject.create= function(value) {"
1.166 + + " var cnt = ++kCnt;"
1.167 + + " var ret = {};"
1.168 + + " ret.toString = function() { return 'KObject' + cnt + ' value: ' + value + ' props: ' + Object.keys(this); };"
1.169 + + " return ret;"
1.170 + + " };"
1.171 + + " scope.KObject.array= function() {"
1.172 + + " return Array.prototype.slice.call(arguments);"
1.173 + + " };"
1.174 + + " scope.KObject.expose = function(bindings, model, prop, sig) {"
1.175 + + " bindings[prop] = function(data, ev) {"
1.176 + // + " console.log(\" callback on prop: \" + prop);"
1.177 + + " model[sig](data, ev);"
1.178 + + " };"
1.179 + + " };"
1.180 + + "})(window); window.KObject");
1.181 + }
1.182 +
1.183 + }
1.184 }