Generic way to applyBindings
authorJaroslav Tulach <jaroslav.tulach@netbeans.org>
Sat, 11 Jan 2014 07:57:44 +0100
changeset 4535e90ec3cd61a
parent 452 059e6c2cc82b
child 454 2e91df8f6114
Generic way to applyBindings
json/src/main/java/net/java/html/json/Models.java
json/src/main/java/org/netbeans/html/json/impl/JSON.java
json/src/test/java/net/java/html/json/ModelTest.java
     1.1 --- a/json/src/main/java/net/java/html/json/Models.java	Sat Jan 11 06:10:42 2014 +0100
     1.2 +++ b/json/src/main/java/net/java/html/json/Models.java	Sat Jan 11 07:57:44 2014 +0100
     1.3 @@ -124,4 +124,21 @@
     1.4          }
     1.5          return JSON.find(model);
     1.6      }
     1.7 +    
     1.8 +    /** Apply bindings of a model class. Each model class has an
     1.9 +     * apply bindings method that "activates" it. In <em>ko4j</em> mode,
    1.10 +     * it binds the model values to the currently active page. One 
    1.11 +     * can call the generated method directly, or use this static 
    1.12 +     * helper method to perform the activation.
    1.13 +     * 
    1.14 +     * @param model instance of a {@link Model class}
    1.15 +     * @throws IllegalArgumentException if the <code>model</code> is not
    1.16 +     * instance of a class generated by {@link Model model annotation}
    1.17 +     * processor.
    1.18 +     * 
    1.19 +     * @since 0.7
    1.20 +     */
    1.21 +    public static void applyBindings(Object model) {
    1.22 +        JSON.applyBindings(model);
    1.23 +    }
    1.24  }
     2.1 --- a/json/src/main/java/org/netbeans/html/json/impl/JSON.java	Sat Jan 11 06:10:42 2014 +0100
     2.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/JSON.java	Sat Jan 11 07:57:44 2014 +0100
     2.3 @@ -269,22 +269,35 @@
     2.4          if (object instanceof Collection) {
     2.5              return JSONList.koData((Collection<?>) object, model);
     2.6          }
     2.7 -        Proto.Type<?> type = JSON.findType(object.getClass());
     2.8 -        if (type == null) {
     2.9 -            return null;
    2.10 -        }
    2.11 -        final Proto proto = PropertyBindingAccessor.protoFor(type, object);
    2.12 +        Proto proto = findProto(object);
    2.13          if (proto == null) {
    2.14              return null;
    2.15          }
    2.16          final Bindings b = PropertyBindingAccessor.getBindings(proto, true);
    2.17          return b == null ? null : b.koData();
    2.18      }
    2.19 +    
    2.20 +    private static Proto findProto(Object object) {
    2.21 +        Proto.Type<?> type = JSON.findType(object.getClass());
    2.22 +        if (type == null) {
    2.23 +            return null;
    2.24 +        }
    2.25 +        final Proto proto = PropertyBindingAccessor.protoFor(type, object);
    2.26 +        return proto;
    2.27 +    }
    2.28  
    2.29      public static Object find(Object object) {
    2.30          return find(object, null);
    2.31      }
    2.32      
    2.33 +    public static void applyBindings(Object object) {
    2.34 +        final Proto proto = findProto(object);
    2.35 +        if (proto == null) {
    2.36 +            throw new IllegalArgumentException("Not a model: " + object.getClass());
    2.37 +        }
    2.38 +        proto.applyBindings();
    2.39 +    }
    2.40 +    
    2.41      public static void loadJSON(
    2.42          BrwsrCtx c, RcvrJSON callback,
    2.43          String urlBefore, String urlAfter, String method,
     3.1 --- a/json/src/test/java/net/java/html/json/ModelTest.java	Sat Jan 11 06:10:42 2014 +0100
     3.2 +++ b/json/src/test/java/net/java/html/json/ModelTest.java	Sat Jan 11 07:57:44 2014 +0100
     3.3 @@ -119,7 +119,7 @@
     3.4      }
     3.5      
     3.6      @Test public void arrayChangesNotified() {
     3.7 -        model.applyBindings();
     3.8 +        Models.applyBindings(model);
     3.9          model.getNames().add("Hello");
    3.10          
    3.11          assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);