TornadoFX将域类包装到ItemViewModel

时间:2017-07-21 15:12:41

标签: kotlin tornadofx

我有一个域类

class UserItem(var simpleItem: Item, var id: String, var status: String, var price: Int) {
    var upperLimit: Int = 0
    var lowerLimit: Int = 0
    /* methods here */
}

以及存储这些对象的类

class Trades {
    val saleableItems: ArrayList<UserItem> = ArrayList()
    val soldAwaitingItems: ArrayList<UserItem> = ArrayList()

    /* methods */
}

saleableItemssoldAwaitingItems已在Trades

内更新

我想使用saleableItems显示TableView,以便我可以绑定数据和View而不将属性转换为TornadoFX属性,因此我将UserItem包裹到ItemViewModel

class UserItemModel(uItem: UserItem) : ItemViewModel<UserItem>() {
    val simpleItem = bind { item?.observable(UserItem::simpleItem) }
    val id = bind { item?.observable(UserItem::id) }
    val status = bind { item?.observable(UserItem::status) }
    val price = bind { item?.observable(UserItem::price) }
    val upperLimit = bind { item?.observable(UserItem::upperLimit) }
    val lowerLimit = bind { item?.observable(UserItem::lowerLimit) }

    init {
        item = uItem
    }
}

有没有办法将Trades包裹到ItemViewModel<Trades>,以便我有类似

的内容
class TradesModel : ItemViewModel<Trades>() {
    val saleableItems: ObservableList<UserItemModel>
    val soldAwaitingItems: ObservableList<UserItemModel>
}

然后在View

中使用它
class TradesView : View("My View") {
val tradesModel: TradesModel by inject()

    tableview(tradesModel.saleableItems) {
        column("Name", UserItemModel::simpleItem)
        column("Price", UserItemModel::price)
        column("Lower limit", UserItemModel::lowerLimit)
        column("Upper limit", UserItemModel::upperLimit)
    }
}

1 个答案:

答案 0 :(得分:1)

你尝试过这样的事吗?

class UserItemModel(uItem: UserItem) : ItemViewModel<UserItem>() {
    // Same way you defined previously.
}

class TradesViewModel {
    val saleableItems: SimpleListProperty<UserItemModel>(FXCollections.observableArrayList())
    val soldAwaitingItems: SimpleListProperty<UserItemModel>(FXCollections.observableArrayList())
}

class TradesView : View("My View") {
    val tradesModel: TradesViewModel by inject()

    init {
        with(root) {
            tableview(tradesModel.saleableItems) {
                column("Name", UserItemModel::simpleItem)
                column("Price", UserItemModel::price)
                column("Lower limit", UserItemModel::lowerLimit)
                column("Upper limit", UserItemModel::upperLimit)
            }   
        }
    }
}

此外,如果您的表是只读的,您可以将UserItemModel定义为一个简单的数据类 - 无需扩展ItemViewModel或执行属性绑定。