mvvm
This commit is contained in:
parent
69f424f013
commit
cfde85b773
@ -1,52 +1,71 @@
|
|||||||
import { VMPanel, View, ViewModel, WRAP_CONTENT, Gravity, Mutable, NativeCall, Text, Color, VLayout, Panel, log, logw, loge, Group, Stack, } from "./index"
|
import { ViewHolder, VMPanel, View, ViewModel, WRAP_CONTENT, Gravity, Mutable, NativeCall, Text, Color, VLayout, Panel, log, logw, loge, Group, Stack, } from "./index"
|
||||||
|
|
||||||
interface CountModel {
|
interface CountModel {
|
||||||
count: number
|
count: number
|
||||||
add: () => void
|
add: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
class CounterVM extends ViewModel<CountModel> {
|
class CounterView extends ViewHolder {
|
||||||
build(root: Group, model: CountModel) {
|
number = new Text
|
||||||
|
counter = new Text
|
||||||
|
build(root: Group) {
|
||||||
const vlayout = new VLayout
|
const vlayout = new VLayout
|
||||||
const number = new Text
|
this.number.textSize = 40
|
||||||
number.textSize = 40
|
this.number.layoutConfig = {
|
||||||
number.layoutConfig = {
|
|
||||||
alignment: new Gravity().center()
|
alignment: new Gravity().center()
|
||||||
}
|
}
|
||||||
const counter = new Text
|
this.counter = new Text
|
||||||
counter.text = "点击计数"
|
this.counter.text = "点击计数"
|
||||||
counter.textSize = 20
|
this.counter.textSize = 20
|
||||||
|
|
||||||
vlayout.space = 20
|
vlayout.space = 20
|
||||||
vlayout.layoutConfig = {
|
vlayout.layoutConfig = {
|
||||||
alignment: new Gravity().center()
|
alignment: new Gravity().center()
|
||||||
}
|
}
|
||||||
vlayout.addChild(number)
|
vlayout.addChild(this.number)
|
||||||
vlayout.addChild(counter)
|
vlayout.addChild(this.counter)
|
||||||
|
|
||||||
root.addChild(vlayout)
|
root.addChild(vlayout)
|
||||||
|
}
|
||||||
|
|
||||||
this.bind((data) => {
|
setNumber(n: number) {
|
||||||
loge('bind:', data)
|
this.number.text = n.toString()
|
||||||
number.text = data.count.toString()
|
}
|
||||||
counter.onClick = data.add
|
|
||||||
})
|
|
||||||
|
|
||||||
|
setCounter(v: Function) {
|
||||||
|
this.counter.onClick = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class CounterVM extends ViewModel<CountModel, CounterView> {
|
||||||
|
binding(v: CounterView, model: CountModel): void {
|
||||||
|
v.setCounter(model.add)
|
||||||
|
v.setNumber(model.count)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Entry
|
@Entry
|
||||||
class MyPage extends VMPanel<CountModel>{
|
class MyPage extends VMPanel<CountModel, CounterView>{
|
||||||
|
|
||||||
createVM(): ViewModel<CountModel> {
|
getVMClass() {
|
||||||
return new CounterVM({
|
return CounterVM
|
||||||
|
}
|
||||||
|
|
||||||
|
getModel() {
|
||||||
|
return {
|
||||||
count: 0,
|
count: 0,
|
||||||
add: function () {
|
add: function () {
|
||||||
this.count++
|
this.count++
|
||||||
}
|
},
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getViewHolder() {
|
||||||
|
return new CounterView
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@NativeCall
|
@NativeCall
|
||||||
log() {
|
log() {
|
||||||
log("Hello.HEGO")
|
log("Hello.HEGO")
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
import { View, Group } from "../ui/view";
|
import { View, Group } from "../ui/view";
|
||||||
import { Panel } from "../ui/panel";
|
import { Panel } from "../ui/panel";
|
||||||
import { loge } from "../util/log";
|
|
||||||
|
|
||||||
|
|
||||||
function listen<T extends Object>(obj: T, listener: Function): T {
|
function listen<T extends Object>(obj: T, listener: Function): T {
|
||||||
return new Proxy(obj, {
|
return new Proxy(obj, {
|
||||||
@ -26,38 +24,52 @@ function listen<T extends Object>(obj: T, listener: Function): T {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class VMPanel<M extends Object> extends Panel {
|
export abstract class ViewHolder {
|
||||||
|
abstract build(root: Group): void
|
||||||
|
}
|
||||||
|
|
||||||
private vm: ViewModel<M> = this.createVM()
|
export abstract class VMPanel<M extends Object, V extends ViewHolder> extends Panel {
|
||||||
|
|
||||||
abstract createVM(): ViewModel<M>
|
private vm: ViewModel<M, V> = new (this.getVMClass())(this.getModel(), this.getViewHolder())
|
||||||
|
|
||||||
|
abstract getVMClass(): new (m: M, v: V) => ViewModel<M, V>
|
||||||
|
|
||||||
|
|
||||||
getModel() {
|
abstract getModel(): M
|
||||||
return this.vm.getModel()
|
|
||||||
}
|
abstract getViewHolder(): V
|
||||||
|
|
||||||
getVM() {
|
getVM() {
|
||||||
return this.vm
|
return this.vm
|
||||||
}
|
}
|
||||||
|
|
||||||
build(root: Group): void {
|
build(root: Group): void {
|
||||||
this.vm.build(root, this.vm.getModel())
|
this.vm.build(root)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class ViewModel<M extends Object> {
|
export abstract class ViewModel<M extends Object, V extends ViewHolder> {
|
||||||
private model: M
|
private model: M
|
||||||
private listeners: Function[] = []
|
private listeners: Function[] = []
|
||||||
constructor(obj: M) {
|
private viewHolder: V
|
||||||
|
|
||||||
|
constructor(obj: M, v: V) {
|
||||||
this.model = listen(obj, () => {
|
this.model = listen(obj, () => {
|
||||||
this.listeners.forEach(e => {
|
this.listeners.forEach(e => {
|
||||||
Reflect.apply(e, this.model, [this.model])
|
Reflect.apply(e, this.model, [this.model])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
this.viewHolder = v
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract build(root: Group, model: M): void
|
build(root: Group) {
|
||||||
|
this.viewHolder.build(root)
|
||||||
|
this.bind((data: M) => {
|
||||||
|
this.binding(this.viewHolder, this.model)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract binding(v: V, model: M): void
|
||||||
|
|
||||||
getModel() {
|
getModel() {
|
||||||
return this.model
|
return this.model
|
||||||
|
Reference in New Issue
Block a user