在 durandal/knockout 中取消(关闭)模态时通知主机页面

Notifying host page when a modal is cancelled (closed) in durandal/knockout

我有一个带有 cancel/close 按钮的模式。

<button type="button" class="close" data-bind="click: close" aria-hidden="true">&times;</button>

当用户使用取消关闭对话框时,我想通知主机页面用户取消(而不是提交)表单。我该怎么做?

当您在 durandal 中打开模式时,方法 returns 一个在对话框关闭时解决的承诺。

示例来自 http://durandaljs.com/documentation/Showing-Message-Boxes-And-Modals.html

app.showMessage('This is a message.').then(function(dialogResult){
    //do something with the dialog result here
});

使用自定义模式时,您会在执行 dialog.close()

时创建自己的 dialogResult

所以你会做这样的事情:

submit: function() {
    dialog.close( this, "submitted" );
}
close: function() {
    dialog.close( this, "closed" );
}

那么在您的主机页面中,您的代码将是:

app.showDialog('views/dialogs/confirm').then(function(dialogResult){
  if( dialogResult == 'closed' ) 
      // User closed instead of submitting
});