How can I send any parameter to change something in the model?
I tried with setters but failed, and when tried to do it from the view I got a strange behaviour.
This is as far as I'm now.
HTML:
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>
<hr>
<a data-bind="text:'hi',click: $root.nameSelected('hi')" href="#"></a>
Java:
package dew.demo.namesmodel;
import net.java.html.json.Model;
import net.java.html.json.Property;
import net.java.html.json.Function;
@Model(className="Names", properties={
@Property(name = "selectedName", type=String.class),
@Property(name = "names", type=String.class, array = true)
})
class NamesModel {
@Function static void nameSelected(Names myModel, String data) {
myModel.setSelectedName(data);
}
static {
Names pageModel = new Names(
"---", "Jarda", "Pepa", "Honza", "Jirka", "Tomáš"
);
pageModel.applyBindings();
}
}
you can see the example running on this fiddle.
It is possible to workaround the problem by using with binding:
<div data-bind="with: 'hi'">
<a data-bind="text: $data,click: $root.nameSelected" href="#"></a>
</div>
as your updated DEW shows.