From 69f424f01387fb1a3085218c2e06aad034b5bf16 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 24 Jul 2019 17:24:00 +0800 Subject: [PATCH] bind view action and model --- js-framework/demo.ts | 18 ++++++++++-------- js-framework/src/vm/mvvm.ts | 7 ++++++- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/js-framework/demo.ts b/js-framework/demo.ts index 021b8da5..65e25c2d 100644 --- a/js-framework/demo.ts +++ b/js-framework/demo.ts @@ -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 { CENTER } from "./src/util/gravity"; - interface CountModel { count: number + add: () => void } class CounterVM extends ViewModel { @@ -23,17 +22,15 @@ class CounterVM extends ViewModel { } vlayout.addChild(number) vlayout.addChild(counter) + root.addChild(vlayout) this.bind((data) => { + loge('bind:', data) number.text = data.count.toString() - loge(`data changed:${data.count},${vlayout.isDirty()}`) + counter.onClick = data.add }) - counter.onClick = () => { - model.count++ - loge('onclick', model.count) - } } } @@ -42,7 +39,12 @@ class CounterVM extends ViewModel { class MyPage extends VMPanel{ createVM(): ViewModel { - return new CounterVM({ count: 0 }) + return new CounterVM({ + count: 0, + add: function () { + this.count++ + } + }) } @NativeCall diff --git a/js-framework/src/vm/mvvm.ts b/js-framework/src/vm/mvvm.ts index 3fff9d90..1d60da82 100644 --- a/js-framework/src/vm/mvvm.ts +++ b/js-framework/src/vm/mvvm.ts @@ -1,12 +1,17 @@ import { View, Group } from "../ui/view"; import { Panel } from "../ui/panel"; +import { loge } from "../util/log"; function listen(obj: T, listener: Function): T { return new Proxy(obj, { 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) } else { return ret