Tornadofx Javafx – 如何重新加载视图/组件

所以它是一个基本的问题。
我想实现的是从另一个角度来看令人耳目一新的观点。

比方说,我有一个视图EmployeeTableView通过执行REST API调用显示员工的表格表示形式。
另一种观点,我有一个过滤器EmployeeFilterView,其中我有性别,工资范围,雇员类型等
我也有一个userContext对象,在其中存储用户首选项。 所以默认情况下可以说我已经存储了性别过滤器的值为Male,工资范围为ALL等。这个对象作为参数发送到EmployeeTableView。

当EmployeeTableView被加载时,我用userContext值做一个restAPI调用来获取员工详细信息。 所以这工作正常。 现在我将性别过滤器更改为女性,并在我的userContext中分配此值。
现在,如果我可以用userContext对象重新加载EmployeeTableView,restapi调用将获得更新的值。

但是我怎么能这样做呢?
如果你有,也建议更好的方法。

EventBus是一个有效的解决方案。 另一个办法是使用一个ViewModel或控制器作为的UserContext对象,并让这包括员工的实际观察到的列表,然后该列表绑定到TableViewEmployeeTableView 。 每当上下文中的列表更新时,TableView也会更新。

过滤器视图将调用UserContext中的函数来执行实际的REST调用,并基于此更新雇员列表。

你可以创建可注入到两个独立的EmployeeQuery对象EmployeeFilterViewUserContext所以它可以提取所选择的过滤器值来进行查询。 该查询对象包含要传递给服务器的所有搜索参数的列表。

你也可以考虑创建一个单独的范围,以保持这些组件分离,如果这是有意义的,你的架构。

确切地说,如何定义这些组件主要是品味的问题,这里有一个建议。 我使用ControlsFX的RangeSlider作为模拟搜索界面。

为了让人们更容易想象如何连接在一起,这里是一个截图:

(所有的名字和薪水都是虚构的:)

员工应用程序

 /** * The employee domain model, implementing JsonModel so it can be fetched * via the REST API */ class Employee : JsonModel { val nameProperty = SimpleStringProperty() var name by nameProperty val salaryProperty = SimpleIntegerProperty() var salary by salaryProperty val genderProperty = SimpleObjectProperty<Gender>() var gender by genderProperty override fun updateModel(json: JsonObject) { with (json) { name = getString("name") salary = getInt("salary") gender = Gender.valueOf(getString("gender")) } } } enum class Gender { Male, Female } /** * Container for the list of employees as well as a search function called by the filter * view whenever it should update the employee list. */ class EmployeeContext : Controller() { val api: Rest by inject() val query: EmployeeQuery by inject() val employees = SimpleListProperty<Employee>() fun search() { runAsync { FXCollections.observableArrayList(Employee().apply { name = "Edvin Syse" gender = Gender.Male salary = 200_000 }) //api.post("employees/query", query).list().toModel<Employee>() } ui { employees.value = it } } } /** * Query object used to define the query sent to the server */ class EmployeeQuery : ViewModel(), JsonModel { val genderProperty = SimpleObjectProperty<Gender>(Gender.Female) var gender by genderProperty val salaryMinProperty = SimpleIntegerProperty(50_000) var salaryMin by salaryMinProperty val salaryMaxProperty = SimpleIntegerProperty(250_000) var salaryMax by salaryMaxProperty val salaryDescription = stringBinding(salaryMinProperty, salaryMaxProperty) { "$$salaryMin - $$salaryMax" } override fun toJSON(json: JsonBuilder) { with(json) { add("gender", gender.toString()) add("salaryMin", salaryMin) add("salaryMax", salaryMax) } } } /** * The search/filter UI */ class EmployeeFilterView : View() { val query: EmployeeQuery by inject() val context: EmployeeContext by inject() override val root = form { fieldset("Employee Filter") { field("Gender") { combobox(query.genderProperty, Gender.values().toList()) } field("Salary Range") { vbox { alignment = Pos.CENTER add(RangeSlider().apply { max = 500_000.0 lowValueProperty().bindBidirectional(query.salaryMinProperty) highValueProperty().bindBidirectional(query.salaryMaxProperty) }) label(query.salaryDescription) } } button("Search").action { context.search() } } } } /** * The UI that shows the search results */ class EmployeeTableView : View() { val context: EmployeeContext by inject() override val root = borderpane { center { tableview(context.employees) { column("Name", Employee::nameProperty) column("Gender", Employee::genderProperty) column("Salary", Employee::salaryProperty) } } } } /** * A sample view that ties the filter UI and result UI together */ class MainView : View("Employee App") { override val root = hbox { add(EmployeeFilterView::class) add(EmployeeTableView::class) } } 

我结束了使用Tornadofx – > EventBus

基本上,当我改变任何过滤器,我发射一个甚至重建与更新值的节点。

不确定这种方法是否正确,这就是为什么还要继续讨论。