UNPKG

mutants-appfx

Version:

appfx module

137 lines (118 loc) 2.62 kB
import { history, clientType } from 'mutants-microfx'; import { action, observable, computed } from 'mobx'; import uifunction from '../UIFunction'; export default class ViewController { module; viewModels; data; blocks; controller; ui; // 统一的UI控制器 @observable _pid; @observable _iid; @computed get viewModel() { const vo = this.viewModels.find(it => it.iid === this._iid); if (!vo) { return null; } return vo.viewModel; } get uifunction() { return this.viewModel ? uifunction.get(this.viewModel.mid) : null; } get clientType() { return clientType; } constructor(module, viewModels, dataModel, ui, controller, blocks) { this.module = module; this.ui = ui; this.controller = controller; this.blocks = blocks; if (viewModels) { this._init(viewModels); } if (dataModel) { this._initData(dataModel); } } @action async _curView(iid, pid) { if (this._iid !== iid) { this._pid = pid; this._iid = iid; await this.loadView(this.viewModel); } } async _init(viewModels) { this.viewModels = viewModels; } async _initData(dataModel) { // 加载表单数据 this.data = dataModel; await this.init(); if (this.data.ID) { await this.load(); } } async executeBlock(name, ctx) { const block = this.blocks[name]; if (!block) { throw new Error(name + ' block not found'); } if (typeof block.execute === 'function') { await block.execute(ctx); return; } else if (typeof block === 'function') { const processors = block({ data: this.data, viewModel: this.viewModel }).asyncProcessors; for (const processor of processors) { await processor.init(ctx).doExecute(); } } else { throw new Error(name + ' block not support!'); } } @action async init() { // 初始化完成 } @action async load() { // 数据已加载 } @action async loadView() { // 视图已加载 } @action async closeView(closedViewModel) { } openUrl({ url }) { history.push(url); } changePage({ pid, state, }) { const { sysid, mid } = this.module; const vi = this.module.views[pid] || this.module.view; if (vi.mode === 'multi') { history.push(`/${sysid}/${mid}?pid=${pid}`, state); return; } if (this._pid === pid) { history.replace(`/${sysid}/${mid}?pid=${pid}`, state); } else { history.push(`/${sysid}/${mid}?pid=${pid}`, state); } } }