Add context to ViewModel

This commit is contained in:
pengfei.zhou 2020-09-21 15:58:45 +08:00 committed by osborn
parent 0f9bed3734
commit e0358a3f81
7 changed files with 11 additions and 2 deletions

View File

@ -3476,6 +3476,7 @@ var VMPanel = /** @class */ (function (_super) {
VMPanel.prototype.build = function (root) {
this.vh = new (this.getViewHolderClass());
this.vm = new (this.getViewModelClass())(this.getState(), this.vh);
this.vm.context = this.context;
this.vm.attach(root);
};
return VMPanel;

View File

@ -2695,6 +2695,7 @@ class VMPanel extends Panel {
build(root) {
this.vh = new (this.getViewHolderClass());
this.vm = new (this.getViewModelClass())(this.getState(), this.vh);
this.vm.context = this.context;
this.vm.attach(root);
}
}

View File

@ -4184,6 +4184,7 @@ class VMPanel extends Panel {
build(root) {
this.vh = new (this.getViewHolderClass());
this.vm = new (this.getViewModelClass())(this.getState(), this.vh);
this.vm.context = this.context;
this.vm.attach(root);
}
}

2
doric-js/index.d.ts vendored
View File

@ -144,11 +144,13 @@ declare module 'doric/lib/src/ui/panel' {
declare module 'doric/lib/src/pattern/mvvm' {
import { Group } from "doric/lib/src/ui/view";
import { Panel } from "doric/lib/src/ui/panel";
import { BridgeContext } from "doric/lib/src/runtime/global";
export abstract class ViewHolder {
abstract build(root: Group): void;
}
export type Setter<M> = (state: M) => void;
export abstract class ViewModel<M extends Object, V extends ViewHolder> {
context: BridgeContext;
constructor(obj: M, v: V);
getState(): M;
getViewHolder(): V;

View File

@ -1,10 +1,12 @@
import { Group } from "../ui/view";
import { Panel } from "../ui/panel";
import { BridgeContext } from "../runtime/global";
export declare abstract class ViewHolder {
abstract build(root: Group): void;
}
export declare type Setter<M> = (state: M) => void;
export declare abstract class ViewModel<M extends Object, V extends ViewHolder> {
context: BridgeContext;
private state;
private viewHolder;
constructor(obj: M, v: V);

View File

@ -29,6 +29,7 @@ export class VMPanel extends Panel {
build(root) {
this.vh = new (this.getViewHolderClass());
this.vm = new (this.getViewModelClass())(this.getState(), this.vh);
this.vm.context = this.context;
this.vm.attach(root);
}
}

View File

@ -15,6 +15,7 @@
*/
import { Group } from "../ui/view"
import { Panel } from "../ui/panel"
import { BridgeContext } from "../runtime/global"
export abstract class ViewHolder {
abstract build(root: Group): void
@ -23,6 +24,7 @@ export abstract class ViewHolder {
export type Setter<M> = (state: M) => void
export abstract class ViewModel<M extends Object, V extends ViewHolder> {
context!: BridgeContext
private state: M
private viewHolder: V
@ -57,10 +59,8 @@ export abstract class ViewModel<M extends Object, V extends ViewHolder> {
export type ClassType<T> = new (...args: any) => T
export abstract class VMPanel<M extends Object, V extends ViewHolder> extends Panel {
private vm?: ViewModel<M, V>
private vh?: V
abstract getViewModelClass(): ClassType<ViewModel<M, V>>
abstract getState(): M
@ -74,6 +74,7 @@ export abstract class VMPanel<M extends Object, V extends ViewHolder> extends Pa
build(root: Group): void {
this.vh = new (this.getViewHolderClass())
this.vm = new (this.getViewModelClass())(this.getState(), this.vh)
this.vm.context = this.context
this.vm.attach(root)
}
}