diff -r d7cff2cba6e5 -r 6dc2c6c752df javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java Thu Apr 18 20:09:45 2013 +0200 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java Thu Apr 18 23:09:30 2013 +0200 @@ -40,28 +40,6 @@ static Knockout next; private final Object model; - static { - BufferedReader r = new BufferedReader(new InputStreamReader(Knockout.class.getResourceAsStream("knockout-2.2.1.js"))); - StringBuilder sb = new StringBuilder(); - for (;;) { - try { - String l = r.readLine(); - if (l == null) { - break; - } - sb.append(l).append('\n'); - } catch (IOException ex) { - throw new IllegalStateException(ex); - } - } - web().executeScript(sb.toString()); - Object ko = web().executeScript("ko"); - assert ko != null : "Knockout library successfully defined 'ko'"; - - Console.register(web()); - } - - Knockout(Object model) { this.model = model == null ? this : model; } @@ -70,43 +48,15 @@ return model; } - private static final JSObject KObject; - static { - KObject = (JSObject) web().executeScript( - "(function(scope) {" - + " var kCnt = 0; " - + " scope.KObject = {};" - + " scope.KObject.create= function(value) {" - + " var cnt = ++kCnt;" - + " var ret = {};" - + " ret.toString = function() { return 'KObject' + cnt + ' value: ' + value + ' props: ' + Object.keys(this); };" - + " return ret;" - + " };" - - + " scope.KObject.array= function() {" - + " return Array.prototype.slice.call(arguments);" - + " };" - - + " scope.KObject.expose = function(bindings, model, prop, sig) {" - + " bindings[prop] = function(data, ev) {" -// + " console.log(\" callback on prop: \" + prop);" - + " model[sig](data, ev);" - + " };" - + " };" - - + "})(window); window.KObject" - ); - } - static Object toArray(Object[] arr) { - return KObject.call("array", arr); + return InvokeJS.KObject.call("array", arr); } public static Knockout applyBindings( Object model, String[] propsGettersAndSetters, String[] methodsAndSignatures ) { - Object bindings = KObject.call("create", model); + Object bindings = InvokeJS.KObject.call("create", model); applyImpl(propsGettersAndSetters, model.getClass(), bindings, model, methodsAndSignatures); return new Knockout(bindings); } @@ -117,7 +67,7 @@ Object bindings = next; next = null; if (bindings == null) { - bindings = KObject.call("create", model); + bindings = InvokeJS.KObject.call("create", model); } applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures); applyBindings(bindings); @@ -167,6 +117,9 @@ Object bindings, Object model, String prop, String getter, String setter, boolean primitive, boolean array ) { WebEngine e = web(); + if (e == null) { + return; + } JSObject bnd = (JSObject) e.executeScript("var x = {}; x.bnd = " + "new Function('ko', 'bindings', 'model', 'prop', 'getter', 'setter', 'primitive', 'array', '" + "var bnd = {" @@ -198,7 +151,7 @@ + "};" + "bindings[prop] = ko.computed(bnd);'" + "); x;"); - + Object ko = e.executeScript("ko"); try { KOProperty kop = new KOProperty(model, strip(getter), strip(setter)); @@ -224,9 +177,13 @@ private static void expose( Object bindings, Object model, String prop, String sig ) { + WebEngine e = web(); + if (e == null) { + return; + } try { KOFunction f = new KOFunction(model, strip(sig)); - KObject.call("expose", bindings, f, prop, "call"); + InvokeJS.KObject.call("expose", bindings, f, prop, "call"); } catch (Throwable ex) { LOG.log(Level.SEVERE, "Cannot define binding for " + prop + " in model " + model, ex); } @@ -234,8 +191,10 @@ @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);") private static void applyBindings(Object bindings) { - JSObject ko = (JSObject) web().executeScript("ko"); - ko.call("applyBindings", bindings); + if (web() != null) { + JSObject ko = (JSObject) web().executeScript("ko"); + ko.call("applyBindings", bindings); + } } private static WebEngine web() { @@ -267,4 +226,50 @@ bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]); } } + + private static final class InvokeJS { + static final JSObject KObject; + + static { + BufferedReader r = new BufferedReader(new InputStreamReader(Knockout.class.getResourceAsStream("knockout-2.2.1.js"))); + StringBuilder sb = new StringBuilder(); + for (;;) { + try { + String l = r.readLine(); + if (l == null) { + break; + } + sb.append(l).append('\n'); + } catch (IOException ex) { + throw new IllegalStateException(ex); + } + } + web().executeScript(sb.toString()); + Object ko = web().executeScript("ko"); + assert ko != null : "Knockout library successfully defined 'ko'"; + + Console.register(web()); + KObject = (JSObject) web().executeScript( + "(function(scope) {" + + " var kCnt = 0; " + + " scope.KObject = {};" + + " scope.KObject.create= function(value) {" + + " var cnt = ++kCnt;" + + " var ret = {};" + + " ret.toString = function() { return 'KObject' + cnt + ' value: ' + value + ' props: ' + Object.keys(this); };" + + " return ret;" + + " };" + + " scope.KObject.array= function() {" + + " return Array.prototype.slice.call(arguments);" + + " };" + + " scope.KObject.expose = function(bindings, model, prop, sig) {" + + " bindings[prop] = function(data, ev) {" + // + " console.log(\" callback on prop: \" + prop);" + + " model[sig](data, ev);" + + " };" + + " };" + + "})(window); window.KObject"); + } + + } }