我有一个自定义的Durandal视图模型,它的方法是:
customModalNewMetric.show(someid);
在我的CustomModalNewMetric
视图模型中,该方法如下所示:
CustomModalNewMetric.show = function(someid) {
return dialog.show(new CustomModalNewMetric(someid));
};
在dialog.show
函数完成后,如何执行某些函数?我想在对话框显示后执行一些jQuery。
发布于 2013-12-21 12:47:57
Durandal的dialog.show()
返回一个promise
,所以你需要做的就是:
CustomModalNewMetric.show = function(someid) {
return dialog.show(new CustomModalNewMetric(someid)).then(function(data) {
//callback function here (called after dialog is closed)
//also will return any passed data
});
}
注意:使用durandal时,请确保:
对于控制台中的任何错误,
编辑(2013年12月24日):
我偶然发现了this example,它的source code说明了我提供的答案。
如果你有任何其他问题,请告诉我!
发布于 2013-12-24 08:57:44
要在对话框打开之后执行后续命令,看起来您需要做的是使用compositionComplete方法(DialogContext.compositionComplete)添加您自己的对话框上下文。
您可以使用dialog.addContext方法添加新的上下文。
在这里,我粗略地修饰了默认上下文,并将其添加为'myContext'
(可能有更优雅的方法来完成此操作):
dialog.addContext('myContext', {
// Retain the behaviour of the original default context
parentContext: dialog.getContext(),
// Nothing but a passthrough to the parent context
addHost: function(theDialog) {
this.parentContext.addHost(theDialog);
},
removeHost: function(theDialog) {
this.parentContext.removeHost(theDialog);
},
attached: function(view) {
this.parentContext.attached(view);
},
// Here we do something different
compositionComplete: function(child, parent, context) {
this.compositionComplete(child, parent, context);
// Do something now that the modal is open
console.log('Modal opened');
}
});
然后,当您打开该对话框时,您需要传递上下文名:
dialog.show(new CustomModalNewMetric(someid), null, 'myContext');
https://stackoverflow.com/questions/20718065
复制相似问题