@Target(value=METHOD) @Retention(value=SOURCE) public @interface Function
Model can be
annotated by this annotation to signal that they should be available
as functions to users of the model classes. The method
should be non-private, static (unless instance mode is on)
and return void.
It may take few arguments. The first argument can be the type of
the associated model class, the other argument can be of any type,
but has to be named data - this one represents the
actual data the function was invoked on. Example:
The above example would render:@Model(className="Names", properties={@Property(name = "selectedName", type=String.class),@Property(name = "names", type=String.class, array = true) }) static class NamesModel {@Functionstatic void nameSelected(Names myModel, String data) { myModel.setSelectedName(data); } static void initialize() { Names pageModel = new Names("---", "Jarda", "Pepa", "Honza", "Jirka", "Tomáš"); pageModel.applyBindings(); } } // associated Knockout HTML page: Selected name: <span data-bind="text: selectedName"></span> <ul data-bind="foreach: names"> <li> <a data-bind="text: $data, click: $root.nameSelected" href="#"></a> </li> </ul>
--- would be replaced
by selected name.
Try this sample on-line!
There can be additional arguments in the method which can extract information
from (typically event object) sent as a second parameter of the function
dispatch method.
Such arguments can be of primitive types (int, double
or String). Their names are used to extract values of appropriate
properties from the event object. The following function...
is called with equivalent of@Functionstatic void meaningOfWorld(Names myModel, String data, int answer) {System.out.println(answer); } // would print 42 if the dispatch method:meaningOfWorld.call(model, json)
var json = { 'answer' : 42 }.Copyright © 2019 The Apache Software Foundation. All rights reserved.