javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java
branchfx
changeset 979 83f4aa79c130
parent 975 094cd25a16d9
child 980 1ea155524be4
     1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java	Sat Apr 13 09:14:54 2013 +0200
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java	Sun Apr 14 11:52:36 2013 +0200
     1.3 @@ -66,12 +66,33 @@
     1.4          this.model = model == null ? this : model;
     1.5      }
     1.6      
     1.7 +    public Object koData() {
     1.8 +        return model;
     1.9 +    }
    1.10 +
    1.11 +    private static final JSObject KObject;
    1.12 +    static {
    1.13 +        KObject = (JSObject) web().executeScript(
    1.14 +            "(function(scope) {"
    1.15 +            + "  var kCnt = 0; "
    1.16 +            + "  scope.KObject = {};"
    1.17 +            + "  scope.KObject.create= function(value) {"
    1.18 +            + "    var cnt = ++kCnt;"
    1.19 +            + "    var ret = {};"
    1.20 +            + "    ret.toString = function() { return 'KObject' + cnt + ' value: ' + value; };"
    1.21 +            + "    return ret;"
    1.22 +            + "  };"
    1.23 +            + "})(window); window.KObject"
    1.24 +            );
    1.25 +    }
    1.26 +    
    1.27      public static <M> Knockout applyBindings(
    1.28          Object model, String[] propsGettersAndSetters,
    1.29          String[] methodsAndSignatures
    1.30      ) {
    1.31 -        applyImpl(propsGettersAndSetters, model.getClass(), model, model, methodsAndSignatures);
    1.32 -        return new Knockout(model);
    1.33 +        Object bindings = KObject.call("create", model);
    1.34 +        applyImpl(propsGettersAndSetters, model.getClass(), bindings, model, methodsAndSignatures);
    1.35 +        return new Knockout(bindings);
    1.36      }
    1.37      public static <M> Knockout applyBindings(
    1.38          Class<M> modelClass, M model, String[] propsGettersAndSetters,
    1.39 @@ -80,7 +101,7 @@
    1.40          Object bindings = next;
    1.41          next = null;
    1.42          if (bindings == null) {
    1.43 -            bindings = web().executeScript("new Object()");
    1.44 +            bindings = KObject.call("create", model);
    1.45          }
    1.46          applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures);
    1.47          applyBindings(bindings);
    1.48 @@ -128,13 +149,23 @@
    1.49          + "  read: function() {"
    1.50          + "    try {"
    1.51          + "      var v = model[getter]();"
    1.52 -        + "      try { v = v.koData(); } catch (ignore) {};"
    1.53 +        + "      console.log(\" getter value \" + v + \" for property \" + prop);"
    1.54 +        + "      try { v = v.koData(); } catch (ignore) {"
    1.55 +        + "        console.log(\"Cannot convert to koData: \" + ignore);"
    1.56 +        + "      };"
    1.57 +        + "      console.log(\" getter ret value \" + v);"
    1.58 +        + "      for (var pn in v) {"
    1.59 +        + "         console.log(\"  prop: \" + pn + \" + in + \" + v + \" = \" + v[pn]);"
    1.60 +        + "         if (typeof v[pn] == \"function\") console.log(\"  its function value:\" + v[pn]());"
    1.61 +        + "      }"
    1.62 +        + "      console.log(\" all props printed for \" + (typeof v));"
    1.63          + "      return v;"
    1.64          + "    } catch (e) {"
    1.65          + "      alert(\"Cannot call \" + getter + \" on \" + model + \" error: \" + e);"
    1.66          + "    }"
    1.67          + "  },"
    1.68 -        + "  owner: bindings"
    1.69 +        + "  owner: bindings,"
    1.70 +        + "  deferEvaluation: true"
    1.71          + "};"
    1.72          + "if (setter != null) {"
    1.73          + "  bnd.write = function(val) {"
    1.74 @@ -148,9 +179,9 @@
    1.75          try {
    1.76              KOProperty kop = new KOProperty(model, strip(getter), strip(setter));
    1.77              bnd.call("bnd", ko, bindings, kop, prop, "get", "set", primitive, array);
    1.78 -            LOG.log(Level.FINE, "binding defined for {0}: {1}", new Object[]{prop, ((JSObject)bindings).getMember(prop)});
    1.79 +            LOG.log(Level.INFO, "binding defined for {0}: {1}", new Object[]{prop, ((JSObject)bindings).getMember(prop)});
    1.80          } catch (Throwable ex) {
    1.81 -            LOG.log(Level.FINE, "binding failed for {0} on {1}", new Object[]{prop, bindings});
    1.82 +            LOG.log(Level.INFO, "binding failed for {0} on {1}", new Object[]{prop, bindings});
    1.83          }
    1.84      }
    1.85