作为深入学习基本JS编程的实践练习(在最新的浏览器上),我正在构建一个SPA来维护客户记录。我使用的唯一外部库是Mithril.js MVC。到目前为止,我已经从我的数据库中获得了一个包含实时数据的表视图,其中包括每个记录的编辑、合并和删除按钮。编辑工作做得很好,并使用内联“表单”,并为此保存/取消。
我现在正在尝试实现删除和合并,这两者都需要弹出确认才能被激活,这正是我陷入困境的地方。我很清楚我在桌面GUI环境中会做些什么,所以障碍可能是我对浏览器前端的理解不足,而不是Mithril本身。
理想情况下,我希望创建一个独立的、可重用的“弹出”组件来表示弹出窗口,但我不知道我应该如何在JS中使用Mithril来实现这一点,特别是,但不仅仅是如何使Mithril将一个视图覆盖在另一个视图之上。
任何帮助都将不胜感激,从一个广泛的大纲到特定的代码片段。
发布于 2014-09-19 01:56:19
您可能希望使用视图模型标志来控制模式弹出的可见性。
//modal module
var modal = {}
modal.visible = m.prop(false)
modal.view = function(body) {
return modal.visible() ? m(".modal", body()) : ""
}
//in your other view
var myOtherView = function() {
//this button sets the flag to true
m("button[type=button]", {onclick: modal.visible.bind(this, true)}, "Show modal"),
//include the modal anywhere it makes sense to
//its visibility is taken care by the modal itself
//positioning is controlled via CSS
modal.view(function() {
return m("p, "modal content goes here")
})
}要建立一个模态对话框,您可以使用许多CSS框架中的一个样式(例如引导),也可以使用您自己的CSS样式.modal。
/*really contrived example to get you started*/
.modal {
background:#fff;
border:1px solid #eee;
position:fixed;
top:10px;
left:100px;
width:600px;
}发布于 2014-10-11 20:42:10
我不知道是否只是没有得到MVC,但我只是设置了一个视图模型对象,其中包含弹出窗口的细节,然后在生成视图时,如果是当前设置的话,我就填充包含弹出窗口的div。CSS控制外观和定位。
因此,基本上,我依赖Mithril自上而下的重呈现方法,根据当前的应用程序状态有条件地构建视图--它工作得很好,而且对我来说具有内在的敏感性。
实际上,我使用了一个弹出确认对象列表,因此多个确认可以排队。
在控制器中,创建一个确认队列:
function Controller() {
...
this.confirmation =[];
...
}在视图中,如果存在确认队列,则创建一个确认视图div,否则创建一个空占位符(如果容器元素不出现并从呈现到呈现消失,则Mithrils差分最有效):
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或转义等)时采取行动--只是将标准UI内容抽象成共享的可重用组件。
注意:如果有人对数组索引有疑问,我就不再使用数组索引来标识数据模型中的记录(当删除完成时,应该删除数组元素)。相反,我使用数据库ID定位受影响的记录,它可以抵御模型中的干预更改,比如排序列表。
https://stackoverflow.com/questions/25920941
复制相似问题