diff -r 000000000000 -r 854286e49061 javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java Sun Jan 20 18:20:18 2013 +0100 @@ -0,0 +1,69 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.htmlpage; + +import org.apidesign.bck2brwsr.core.ExtraJavaScript; +import org.apidesign.bck2brwsr.core.JavaScriptBody; + +/** Provides binding between models and + * + * @author Jaroslav Tulach + */ +@ExtraJavaScript(resource = "org/apidesign/bck2brwsr/htmlpage/knockout-min-2.2.0.js") +public final class Knockout { + + + private Knockout() { + } + + public static void applyBindings( + Class modelClass, M model, String[] propsGettersAndSetters + ) { + Object bindings = new Object(); + for (int i = 0; i < propsGettersAndSetters.length; i += 3) { + bind(bindings, model, propsGettersAndSetters[i], + propsGettersAndSetters[i + 1], + propsGettersAndSetters[i + 2] + ); + } + applyBindings(bindings); + } + + @JavaScriptBody(args = { "bindings", "model", "prop", "getter", "setter" }, body = + "var bnd = {\n" + + " read: function() {\n" + + " var v = model[getter]();\n" + + " return v;\n" + + " },\n" + + " owner: bindings\n" + + "};\n" + + "if (setter != null) {\n" + + " bnd.write = function(val) {\n" + + " model[setter](new Number(val));\n" + + " };\n" + + "}\n" + + "bindings[prop] = ko.computed(bnd);" + ) + private static void bind( + Object bindings, Object model, String prop, String getter, String setter + ) { + } + + @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);") + private static void applyBindings(Object bindings) {} +}