Tornadofx - 在 ViewModel 中保留全局访问的属性?
Tornadofx - Keeping globally accessed properties in a ViewModel?
推理:
大家好。我正在构建一个进化模拟器作为个人项目。我在文本字段上设置了一些参数,例如模拟速度和“有机体”的数量。这些将被应用程序的多个组件访问。因为我还想对一些参数使用验证,所以我设置了一个 ViewModel,如下所示:
class ParametersModel: ViewModel() {
// These properties would likely be DoubleProperty, but for simplicity lets pretend they are strings
val simulationSpeed = bind { SimpleStringProperty() }
val organismsGenerated = bind { SimpleStringProperty() }
}
...然后在文本字段上执行验证测试:
val model = ParametersModel()
textfield(model.simulationSpeed).required()
这工作正常,但问题是我将模型属性定义为绑定到一个空 SimpleDoubleProperty
,这是多余的,因为我从不提交这个模型(程序应该始终在键入时读取更改)。同时,我不能简单地将模型属性定义为:
class ParametersModel: ViewModel() {
val simulationSpeed = SimpleStringProperty()
val organismsGenerated = SimpleStringProperty()
}
因为我随后收到有关验证的错误消息:
The addValidator extension can only be used on inputs that are already bound bidirectionally to a property in a Viewmodel. Use validator.addValidator() instead or make the property's bean field point to a ViewModel.
我可以采取的另一个选择是制作一个名为 GlobalProperties 的 class,它会保留我的属性以及 ValidationContext。然后我可以使用 validationContext.addValidator
添加验证器并传递文本字段。但在这一点上,我觉得我只是在编写一个 ViewModel 的等效代码。
问题:
ViewModel 是保持文本字段设置的“全局”访问参数的正确方法吗?如果是这样,有没有办法不必将模型属性设置为空属性的绑定,因为我不需要提交任何东西?
通常您会使用带有某种模型的 ViewModel
。然后你可以使用 ViewModel
来处理用户输入,它存储用户输入的当前状态,并且假设验证通过(看起来在与你声称“永远不需要做任何事情”的说法相悖)。
像这样:
class Parameters {
val simulationSpeedProperty = SimpleStringProperty(...)
var simulationSpeed by simulationSpeedProperty
val organismsGeneratedProperty = SimpleStringProperty(...)
var organismsGenerated by organismsGeneratedProperty
}
class ParametersModel(parameters: Parameters): ItemViewModel<Parameters>(parameters) {
val simulationSpeed = bind(Parameters::simulationSpeedProperty)
val organismsGenerated = bind(Parameters::organismsGeneratedProperty)
}
那么你可以确定支持 ParametersModel
的 Parameters
总是有有效值(当然假设它是用有效值初始化的)。
推理:
大家好。我正在构建一个进化模拟器作为个人项目。我在文本字段上设置了一些参数,例如模拟速度和“有机体”的数量。这些将被应用程序的多个组件访问。因为我还想对一些参数使用验证,所以我设置了一个 ViewModel,如下所示:
class ParametersModel: ViewModel() {
// These properties would likely be DoubleProperty, but for simplicity lets pretend they are strings
val simulationSpeed = bind { SimpleStringProperty() }
val organismsGenerated = bind { SimpleStringProperty() }
}
...然后在文本字段上执行验证测试:
val model = ParametersModel()
textfield(model.simulationSpeed).required()
这工作正常,但问题是我将模型属性定义为绑定到一个空 SimpleDoubleProperty
,这是多余的,因为我从不提交这个模型(程序应该始终在键入时读取更改)。同时,我不能简单地将模型属性定义为:
class ParametersModel: ViewModel() {
val simulationSpeed = SimpleStringProperty()
val organismsGenerated = SimpleStringProperty()
}
因为我随后收到有关验证的错误消息:
The addValidator extension can only be used on inputs that are already bound bidirectionally to a property in a Viewmodel. Use validator.addValidator() instead or make the property's bean field point to a ViewModel.
我可以采取的另一个选择是制作一个名为 GlobalProperties 的 class,它会保留我的属性以及 ValidationContext。然后我可以使用 validationContext.addValidator
添加验证器并传递文本字段。但在这一点上,我觉得我只是在编写一个 ViewModel 的等效代码。
问题:
ViewModel 是保持文本字段设置的“全局”访问参数的正确方法吗?如果是这样,有没有办法不必将模型属性设置为空属性的绑定,因为我不需要提交任何东西?
通常您会使用带有某种模型的 ViewModel
。然后你可以使用 ViewModel
来处理用户输入,它存储用户输入的当前状态,并且假设验证通过(看起来在与你声称“永远不需要做任何事情”的说法相悖)。
像这样:
class Parameters {
val simulationSpeedProperty = SimpleStringProperty(...)
var simulationSpeed by simulationSpeedProperty
val organismsGeneratedProperty = SimpleStringProperty(...)
var organismsGenerated by organismsGeneratedProperty
}
class ParametersModel(parameters: Parameters): ItemViewModel<Parameters>(parameters) {
val simulationSpeed = bind(Parameters::simulationSpeedProperty)
val organismsGenerated = bind(Parameters::organismsGeneratedProperty)
}
那么你可以确定支持 ParametersModel
的 Parameters
总是有有效值(当然假设它是用有效值初始化的)。