javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java
branchmodel
changeset 909 e51a474fcf79
parent 887 13dc5ada296b
child 969 df08556c5c7c
child 1018 49eb825c87b7
     1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java	Mon Mar 25 16:17:21 2013 +0100
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java	Sun Mar 31 12:01:38 2013 +0200
     1.3 @@ -30,45 +30,40 @@
     1.4  public class Knockout {
     1.5      /** used by tests */
     1.6      static Knockout next;
     1.7 +    private final Object model;
     1.8  
     1.9 -    Knockout() {
    1.10 +    Knockout(Object model) {
    1.11 +        this.model = model == null ? this : model;
    1.12      }
    1.13      
    1.14      public static <M> Knockout applyBindings(
    1.15 +        Object model, String[] propsGettersAndSetters,
    1.16 +        String[] methodsAndSignatures
    1.17 +    ) {
    1.18 +        applyImpl(propsGettersAndSetters, model.getClass(), model, model, methodsAndSignatures);
    1.19 +        return new Knockout(model);
    1.20 +    }
    1.21 +    public static <M> Knockout applyBindings(
    1.22          Class<M> modelClass, M model, String[] propsGettersAndSetters,
    1.23          String[] methodsAndSignatures
    1.24      ) {
    1.25          Knockout bindings = next;
    1.26          next = null;
    1.27          if (bindings == null) {
    1.28 -            bindings = new Knockout();
    1.29 +            bindings = new Knockout(null);
    1.30          }
    1.31 -        for (int i = 0; i < propsGettersAndSetters.length; i += 4) {
    1.32 -            try {
    1.33 -                Method getter = modelClass.getMethod(propsGettersAndSetters[i + 3]);
    1.34 -                bind(bindings, model, propsGettersAndSetters[i],
    1.35 -                    propsGettersAndSetters[i + 1],
    1.36 -                    propsGettersAndSetters[i + 2],
    1.37 -                    getter.getReturnType().isPrimitive(),
    1.38 -                    List.class.isAssignableFrom(getter.getReturnType())
    1.39 -                );
    1.40 -            } catch (NoSuchMethodException ex) {
    1.41 -                throw new IllegalStateException(ex.getMessage());
    1.42 -            }
    1.43 -        }
    1.44 -        for (int i = 0; i < methodsAndSignatures.length; i += 2) {
    1.45 -            expose(
    1.46 -                bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]
    1.47 -            );
    1.48 -        }
    1.49 +        applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures);
    1.50          applyBindings(bindings);
    1.51          return bindings;
    1.52      }
    1.53  
    1.54 -    @JavaScriptBody(args = { "prop" }, body =
    1.55 -        "this[prop].valueHasMutated();"
    1.56 +    public void valueHasMutated(String prop) {
    1.57 +        valueHasMutated(model, prop);
    1.58 +    }
    1.59 +    @JavaScriptBody(args = { "self", "prop" }, body =
    1.60 +        "self[prop].valueHasMutated();"
    1.61      )
    1.62 -    public void valueHasMutated(String prop) {
    1.63 +    public void valueHasMutated(Object self, String prop) {
    1.64      }
    1.65      
    1.66  
    1.67 @@ -107,4 +102,29 @@
    1.68      
    1.69      @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
    1.70      private static void applyBindings(Object bindings) {}
    1.71 +    
    1.72 +    private static void applyImpl(
    1.73 +        String[] propsGettersAndSetters,
    1.74 +        Class<?> modelClass,
    1.75 +        Object bindings,
    1.76 +        Object model,
    1.77 +        String[] methodsAndSignatures
    1.78 +    ) throws IllegalStateException, SecurityException {
    1.79 +        for (int i = 0; i < propsGettersAndSetters.length; i += 4) {
    1.80 +            try {
    1.81 +                Method getter = modelClass.getMethod(propsGettersAndSetters[i + 3]);
    1.82 +                bind(bindings, model, propsGettersAndSetters[i],
    1.83 +                    propsGettersAndSetters[i + 1],
    1.84 +                    propsGettersAndSetters[i + 2],
    1.85 +                    getter.getReturnType().isPrimitive(),
    1.86 +                    List.class.isAssignableFrom(getter.getReturnType()));
    1.87 +            } catch (NoSuchMethodException ex) {
    1.88 +                throw new IllegalStateException(ex.getMessage());
    1.89 +            }
    1.90 +        }
    1.91 +        for (int i = 0; i < methodsAndSignatures.length; i += 2) {
    1.92 +            expose(
    1.93 +                bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]);
    1.94 +        }
    1.95 +    }
    1.96  }