Sending constant parameters to change a model field in dukescript

78 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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.

1
On

The example seems to fail only in DEW. I created a normal DukeScript project, where it works fine, also when running in a browser. So I assume this is simply a bug in DEW.