bind view action and model
This commit is contained in:
parent
43fd9fedd1
commit
69f424f013
@ -1,8 +1,7 @@
|
|||||||
import { VMPanel, View, ViewModel, WRAP_CONTENT, Gravity, Mutable, NativeCall, Text, Color, VLayout, Panel, log, logw, loge, Group, Stack, } from "./index"
|
import { VMPanel, View, ViewModel, WRAP_CONTENT, Gravity, Mutable, NativeCall, Text, Color, VLayout, Panel, log, logw, loge, Group, Stack, } from "./index"
|
||||||
import { CENTER } from "./src/util/gravity";
|
|
||||||
|
|
||||||
interface CountModel {
|
interface CountModel {
|
||||||
count: number
|
count: number
|
||||||
|
add: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
class CounterVM extends ViewModel<CountModel> {
|
class CounterVM extends ViewModel<CountModel> {
|
||||||
@ -23,26 +22,29 @@ class CounterVM extends ViewModel<CountModel> {
|
|||||||
}
|
}
|
||||||
vlayout.addChild(number)
|
vlayout.addChild(number)
|
||||||
vlayout.addChild(counter)
|
vlayout.addChild(counter)
|
||||||
|
|
||||||
root.addChild(vlayout)
|
root.addChild(vlayout)
|
||||||
|
|
||||||
this.bind((data) => {
|
this.bind((data) => {
|
||||||
|
loge('bind:', data)
|
||||||
number.text = data.count.toString()
|
number.text = data.count.toString()
|
||||||
loge(`data changed:${data.count},${vlayout.isDirty()}`)
|
counter.onClick = data.add
|
||||||
})
|
})
|
||||||
counter.onClick = () => {
|
|
||||||
model.count++
|
|
||||||
loge('onclick', model.count)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Entry
|
@Entry
|
||||||
class MyPage extends VMPanel<CountModel>{
|
class MyPage extends VMPanel<CountModel>{
|
||||||
|
|
||||||
createVM(): ViewModel<CountModel> {
|
createVM(): ViewModel<CountModel> {
|
||||||
return new CounterVM({ count: 0 })
|
return new CounterVM({
|
||||||
|
count: 0,
|
||||||
|
add: function () {
|
||||||
|
this.count++
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@NativeCall
|
@NativeCall
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
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, {
|
||||||
get: (target, prop, receiver) => {
|
get: (target, prop, receiver) => {
|
||||||
const ret = Reflect.get(target, prop, receiver)
|
const ret = Reflect.get(target, prop, receiver)
|
||||||
if (ret instanceof Object) {
|
if (ret instanceof Function) {
|
||||||
|
return () => {
|
||||||
|
return Reflect.apply(ret, receiver, arguments)
|
||||||
|
}
|
||||||
|
} else if (ret instanceof Object) {
|
||||||
return listen(ret, listener)
|
return listen(ret, listener)
|
||||||
} else {
|
} else {
|
||||||
return ret
|
return ret
|
||||||
|
Reference in New Issue
Block a user