Verify boolena bindings
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 16 May 2013 13:40:26 +0200
changeset 9406fd367604d0
parent 93 01575d7bd3ff
child 95 045b0f7347cf
Verify boolena bindings
json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java
ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/Knockout.java
     1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java	Wed May 15 11:45:03 2013 +0200
     1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java	Thu May 16 13:40:26 2013 +0200
     1.3 @@ -37,7 +37,8 @@
     1.4      @Property(name="name", type=String.class),
     1.5      @Property(name="results", type=String.class, array = true),
     1.6      @Property(name="callbackCount", type=int.class),
     1.7 -    @Property(name="people", type=PersonImpl.class, array = true)
     1.8 +    @Property(name="people", type=PersonImpl.class, array = true),
     1.9 +    @Property(name="enabled", type=boolean.class)
    1.10  }) 
    1.11  public final class KnockoutTest {
    1.12      
    1.13 @@ -98,6 +99,22 @@
    1.14          assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
    1.15          assert "Hi".equals(m.getName()) : "We got callback from 2nd child " + m.getName();
    1.16      }
    1.17 +
    1.18 +    @HtmlFragment(
    1.19 +        "<input type='checkbox' id='b' data-bind='checked: enabled'></input>\n"
    1.20 +    )
    1.21 +    @BrwsrTest public void checkBoxToBooleanBinding() throws Exception {
    1.22 +        KnockoutModel m = new KnockoutModel(Utils.newContext());
    1.23 +        m.applyBindings();
    1.24 +        
    1.25 +        assert !m.isEnabled() : "Is disabled";
    1.26 +
    1.27 +        triggerClick("b");
    1.28 +        
    1.29 +        assert m.isEnabled() : "Now the model is enabled";
    1.30 +    }
    1.31 +    
    1.32 +    
    1.33      
    1.34      @HtmlFragment(
    1.35          "<ul id='ul' data-bind='foreach: cmpResults'>\n"
    1.36 @@ -237,6 +254,14 @@
    1.37          )).intValue();
    1.38      }
    1.39  
    1.40 +    private static void triggerClick(String id) throws Exception {
    1.41 +        String s = "var id = arguments[0];"
    1.42 +            + "var e = window.document.getElementById(id);\n "
    1.43 +            + "var ev = window.document.createEvent('MouseEvents');\n "
    1.44 +            + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
    1.45 +            + "e.dispatchEvent(ev);\n ";
    1.46 +        Utils.executeScript(s, id);
    1.47 +    }
    1.48      private static void triggerChildClick(String id, int pos) throws Exception {
    1.49          String s = "var id = arguments[0]; var pos = arguments[1];"
    1.50              + "var e = window.document.getElementById(id);\n "
     2.1 --- a/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/Knockout.java	Wed May 15 11:45:03 2013 +0200
     2.2 +++ b/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/Knockout.java	Thu May 16 13:40:26 2013 +0200
     2.3 @@ -78,14 +78,15 @@
     2.4            "var bnd = {\n"
     2.5          + "  'read': function() {\n"
     2.6          + "    var v = model[getter]();\n"
     2.7 -        + "    if (array) v = v.koArray();\n"
     2.8 +        + "    if (array) v = v.koArray(); else if (v !== null) v = v.valueOf();\n"
     2.9          + "    return v;\n"
    2.10          + "  },\n"
    2.11          + "  'owner': bindings\n"
    2.12          + "};\n"
    2.13          + "if (setter != null) {\n"
    2.14          + "  bnd['write'] = function(val) {\n"
    2.15 -        + "    model[setter](primitive ? new Number(val) : val);\n"
    2.16 +        + "    var v = val === null ? null : val.valueOf();"
    2.17 +        + "    model[setter](v);\n"
    2.18          + "  };\n"
    2.19          + "}\n"
    2.20          + "bindings[prop] = ko['computed'](bnd);"