Richer example showing Knockout operations with arrays/lists and ability to receive parameters from an event
4 import org.apidesign.bck2brwsr.htmlpage.api.*;
5 import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
6 import org.apidesign.bck2brwsr.htmlpage.api.Page;
7 import org.apidesign.bck2brwsr.htmlpage.api.Property;
8 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
10 /** This is the controller class for associated index.html page. The <code>Index</code>
11 * is autogenerated by parsing the index.html page. It fields represent individual
12 * elements annotated by "id" in the page.
14 @Page(xhtml="index.html", className="Index", properties={
15 @Property(name="name", type=String.class),
16 @Property(name="messages", type=String.class, array=true),
20 Index model = new Index();
21 model.setName("World");
22 model.applyBindings();
26 * @param m the model of the index page creates in static initializer
28 @On(event = CLICK, id="hello")
29 static void hello(Index m) {
30 display(m.getHelloMessage(), m);
31 m.getMessages().add(m.getHelloMessage());
34 /** Reacts when mouse moves over the canvas.
36 * @param m the model of the page
37 * @param x property "x" extracted from the event generated by the browser
38 * @param y property "y" from the mouse event
40 @On(event = MOUSE_MOVE, id="canvas")
41 static void clearPoint(Index m, int x, int y) {
42 GraphicsContext g = m.canvas.getContext();
43 boolean even = (x + y) % 2 == 0;
45 g.setFillStyle("blue");
47 g.setFillStyle("red");
49 g.clearRect(0, 0, 1000, 1000);
50 g.setFont("italic 40px Calibri");
51 g.fillText(m.getHelloMessage(), 10, 40);
54 /** Callback function called by the KnockOut/Java binding on elements
55 * representing href's with individual messages being their data.
57 * @param data the data associated with the element
58 * @param m the model of the page
61 static void display(String data, Index m) {
62 GraphicsContext g = m.canvas.getContext();
63 g.clearRect(0, 0, 1000, 1000);
64 g.setFillStyle("black");
65 g.setFont("italic 40px Calibri");
66 g.fillText(data, 10, 40);
69 /** Callback function.
71 * @param data data associated with the actual element on the page
72 * @param m the model of the page
75 static void remove(String data, Index m) {
76 m.getMessages().remove(data);
80 static String helloMessage(String name) {
81 return "Hello " + name + "!";
85 static boolean noMessages(List<String> messages) {
86 return messages.isEmpty();