This is a migrated thread and some comments may be shown as answers.

How do I cancel the Window OnClose event?

3 Answers 175 Views
Window
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jonathan
Top achievements
Rank 1
Jonathan asked on 17 Mar 2011, 01:06 AM
I have developed a plugin that checks for dirty fields on any attempt to navigate away from a form with changes. To do this I bind my own event to the $('a.t-window-action .t-close') element (among other potential nav items). The event hander checks to see if the form is dirty, and if it is it interrupts the closing of the Window and asks for confirmation (this is working). If the user selects cancel (meaning they don't want to close the window) I call the jQuery normalized stopImmediatePropagation method of the click event. This works for every link but the window close (X) link. So how do I cancel the Window close event?

Here's what my event handler looks like:

var attemptNavigation = function(e) {
    if (listener.checkForChanges()) {
        if (confirm('Are you sure you want to abandon your changes?')) {
            container.dirtyFormListener('destroy');
        } else {
            e.preventDefault();
            e.stopImmediatePropagation();
        }
    }
}

Any help will be greatly appreciated.

Cheers,
Jonathan

3 Answers, 1 is accepted

Sort by
0
Jonathan
Top achievements
Rank 1
answered on 17 Mar 2011, 01:49 AM
OK. I found out that if I define the Window OnClose event I can cancel it from there, but doing it that way would totally wreck the encapsulation of the plugin. Are there any alternatives?
0
Jonathan
Top achievements
Rank 1
answered on 17 Mar 2011, 07:47 PM
I'm talking to myself here, but this is how I ended up cancelling the close event without adding an OnClose client event to the Window:

// simple jQuery plugin
$.fn.bindFirst = function(bindType, event, namespace, handler, selector) {
    var namespacedEvent = event + (namespace.length > 0 ? '.' + namespace : ''),
       events;
 
    // Unbind before binding to prevent duplicates
    if (bindType == 'delegate') {
        this.undelegate(selector, namespacedEvent, handler)
           .delegate(selector, namespacedEvent, handler);
        event = 'live';
    } else {
        this.unbind(namespacedEvent, handler)
           .bind(namespacedEvent, handler);
    }
 
    events = this.data('events')[event];
    if (events != undefined && events.length > 1) {
        events.unshift(events.pop());
        this.data('events')[event] = events;
    }
 
    return this;
}
 
...
 
// code w/in my dirtyFormListener plugin
container.closest('.t-window').bindFirst('delegate', 'click', eventNamespace, function() { ... }, '.t-close');
 
...
0
Tej
Top achievements
Rank 1
answered on 16 Oct 2011, 01:15 AM
Hi Jonathan
I am looking for exactly same functionality for one of my applications. I am using Telerik Window to show some forms. I need to check if the form is dirty and popup an confirm message for users when they attempt to close the window. If they choose to cancel closing the event, i want to cancel the OnClose event.
I would appreciate if you could post an working example of your solutions.
Thanks in advance.
Tej
Tags
Window
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Jonathan
Top achievements
Rank 1
Tej
Top achievements
Rank 1
Share this question
or