尝试插入与模型无关的数据会产生 null
Trying to insert data non related to model produces null
我正在尝试键入一些与基于 this example 的模型没有直接关系的数字。
我的代码
HTML:
<input data-bind="value: screenText" />
<div>
<div>
<div data-bind="with: '1'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '2'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '3'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
</div>
<div>
<div data-bind="with: '4'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '5'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '6'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
</div>
<div>
<div data-bind="with: '7'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '8'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '9'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
</div>
</div>
<a>Llamar</a>
<div data-bind="with: 'hi'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
JAVA:
package dew.demo.namesmodel;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.java.html.json.Model;
import net.java.html.json.Property;
import net.java.html.json.Function;
@Model(className = "Data", properties = {
@Property(name = "screenText", type = String.class)
})
class DataModel {
@Function
static void keyPushed(Data model, String keyPress) {
System.out.println(keyPress);
model.setScreenText(model.getScreenText()+keyPress);
}
static {
Data ui = new Data();
ui.setScreenText("1");
ui.applyBindings();
}
}
您可能已经注意到,我们的想法是将输入用作显示并在单击时附加键值。
¿这可能吗,还是我必须采用强制将其包含到数据模型中的方法?
提前致谢。
在您的函数中,您正在使用 with-binding 设置 $data。所以你想在你的函数中访问 $data 。这样做:
@Function
static void keyPushed(Data model, String data) {
System.out.println(data);
model.setScreenText(model.getScreenText()+ data);
}
我正在尝试键入一些与基于 this example 的模型没有直接关系的数字。
我的代码
HTML:
<input data-bind="value: screenText" />
<div>
<div>
<div data-bind="with: '1'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '2'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '3'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
</div>
<div>
<div data-bind="with: '4'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '5'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '6'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
</div>
<div>
<div data-bind="with: '7'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '8'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '9'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
</div>
</div>
<a>Llamar</a>
<div data-bind="with: 'hi'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
JAVA:
package dew.demo.namesmodel;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.java.html.json.Model;
import net.java.html.json.Property;
import net.java.html.json.Function;
@Model(className = "Data", properties = {
@Property(name = "screenText", type = String.class)
})
class DataModel {
@Function
static void keyPushed(Data model, String keyPress) {
System.out.println(keyPress);
model.setScreenText(model.getScreenText()+keyPress);
}
static {
Data ui = new Data();
ui.setScreenText("1");
ui.applyBindings();
}
}
您可能已经注意到,我们的想法是将输入用作显示并在单击时附加键值。
¿这可能吗,还是我必须采用强制将其包含到数据模型中的方法?
提前致谢。
在您的函数中,您正在使用 with-binding 设置 $data。所以你想在你的函数中访问 $data 。这样做:
@Function
static void keyPushed(Data model, String data) {
System.out.println(data);
model.setScreenText(model.getScreenText()+ data);
}