我不知道我是否只是不太了解 MVC,但我只是设置了一个包含弹出窗口细节的视图模型对象,然后在生成视图时(如果当前已设置)我填充包含弹出窗口的 div。CSS 控制外观和定位。
所以基本上我依靠 Mithril 的自上而下的重新渲染方法来有条件地基于当前应用程序状态构建视图——它工作得非常好,并且对我来说非常明智。
我实际上使用了一个弹出确认对象列表,因此多个确认可以排队。
在控制器中,创建一个确认队列:
function Controller() {
...
this.confirmation =[];
...
}
在视图中,div
如果有确认队列,则创建一个确认视图,否则创建一个空占位符(如果容器元素在渲染之间没有出现和消失,秘银差分效果最好):
function crtView(ctl) {
...
return m("div", [
...
crtConfirmationView(ctl),
...
]);
}
function crtConfirmationView(ctl) {
var cfm=ctl.confirmation[0];
return m("div#popup-confirm",(cfm ? muiConfirm.crtView(ctl,cfm.title,cfm.body,cfm.buttons) : null));
}
然后,每当需要确认时,只需将确认对象推入队列,让 Mithril 的绘图系统运行并重建视图。
function deleteRecord(ctl,evt,row,idx,rcd) {
var cfm={
title : m("span","Delete Customer: "+rcd.ContactName),
body : [
m("p","Do you really want to delete customer "+rcd.CustomerId+" ("+rcd.ContactName+") and all associated appointments and addresses?"),
m("p.warning", "This action cannot be undone. If this is a duplicate customer, it should be merged with the other record."),
],
buttons : deleteButtons,
proceed : "delete",
index : idx,
record : rcd,
};
ctl.confirmation.push(cfm);
}
确认对象包含confirm
帮助函数crtView
创建确认视图所需的任何属性,然后在用户单击按钮(或按 ENTER 或 ESCAPE 等)时执行操作——只是您抽象为共享可重用组件的标准 UI 内容。
注意:以防万一有人对数组索引有疑问,我已经不再使用数组索引来识别数据模型中的记录(当删除完成时,应该删除数组元素)。相反,我使用数据库 ID 定位受影响的记录,该 ID 可以抵御模型中的干预更改,例如对列表进行排序。