0

How to get clicked element text trough javascript's event.target.value?.

I found this knockoutjs example and want to port it to Dukescript.

Here's my try:

HTML

<div class='liveExample'>   
    <h2 class="outmodel" data-bind="value: 'A', click: myFunction">Aaaaa</h2>  
    <h2 class="outmodel" data-bind="value: 'B', click: myFunction">Bbbbb</h2>
    <h2 class="outmodel" data-bind="value: 'C', click: myFunction">Ccccc</h2>
  <hr />
  <h1 data-bind="text: say"></h1>
</div>

Java:

package dew.demo.ko4j;

import net.java.html.json.*;
import net.java.html.js.*;

@Model(className="Hello", properties={
  @Property(name="say", type=String.class)
})
final class HelloViaKO {

  @Function public static void myFunction(Hello model){
    model.setSay(getTextValue());
  } 

  @JavaScriptBody(args={}, body = "return event.target.value;")
  private static native String getTextValue();

  static {
    Hello model = new Hello("Hello World!");
    model.applyBindings();
  }
}

And you can see it on this Fiddle, if you look at the console appears the message

java.lang.Throwable: ReferenceError: event is not defined

since java.awt.Event is nor present how can we reference the event like in this.myFunction = function (data, event)?

Ruslan López
  • 4,433
  • 2
  • 26
  • 37

2 Answers2

1

You should use

@Function public static void myFunction(Hello model, String data) {
  model.setSay(data);
}

as the classical DEW sample does.

Jaroslav Tulach
  • 519
  • 4
  • 7
0

Again, this example works perfectly fine in a regular DukeScript project, but it fails in DEW. (Probably time to update DEW)

monacotoni
  • 606
  • 5
  • 18