Musical Shore
Musical Shore

Reputation: 1381

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

I have a modal with a cancel/close button.

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

When the user closes the dialog using cancel, I'd like to notify the host page that the user canceled (as opposed to submitted) the form. How can I do this?

Upvotes: 0

Views: 84

Answers (1)

Charles
Charles

Reputation: 3774

When you open a modal in durandal the method returns a promise which is resolved when the dialog is closed.

An example from 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
});

When using a custom modal, you create your own dialogResult when you execute dialog.close()

So you would do something like:

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

Then in your host page your code would be:

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

Upvotes: 1

Related Questions