views:

295

answers:

2

When a user requests to edit an entry in our CMS we 'lock' it so that nobody else can edit it simultaneously, we release the lock when they submit their changes. However we need to handle the case where the user leaves the page through other links... my first attempt is to use jQuery to fire a synchronous $.ajax() call on $(window).unload. This does work, however on the next page the user sees, the entry appears to be locked (assuming they return to the page listing all the entries, perhaps via the back button). A simple refresh of that page shows the entry is ready for editing again.

My guess here would be that for whatever reason the browser is fetching the next page before the ajax request has been completely processed. The question is whether there is a way to ensure things happen in the correct order.

var fire_unload_ajax = true; // certain things set this to false, not relevant
$(window).unload(function() {
    if(fire_unload_ajax && $("#reset-entry-lock form").length == 1) {
        $.ajax({
            url: window.location.href,
            async: false,
            cache: false,
            type: "POST",
            data: $("#reset-entry-lock form").serialize()
        });
    }
});
A: 

You could send the state of the form using an asynchronous AJAX POST every five minutes or so, like Gmail does when writing a message. As I explained in my comment, you can't rely on onbeforeunload.

Marcel Korpel
the problem isn't saving what they've done, the problem is editing the DB if they leave the page without wanting to save what they've done. If they close the browser window, hit the back button, navigate to a new url via a link or manually, etc... we need to update the DB to free the lock on that entry. Until/unless a better answer is found we've used onbeforeunload and opera users will just have to free their locks manually.
Ty W
+1  A: 

I solved a similar problem in an app I wrote recently.

I ended up with code in the load event of every page in my app that checked to see if the user still had locks and released them if they were no longer required. I also had code like yours that released locks on an unload event, in case the user was navigating to a page outside my app.

For Opera users (etc) I had intended to have locks automatically expire every minute or so, and do an async postback from my app every 30 seconds to renew the locks. That way, users who navigated elsewhere would have their locks freed even if the unload event was not fired. Unfortunately, my project was cancelled before this was implemented.

Kramii
good thoughts, sorry about your project. I was doing this as a small consulting job so I probably wouldn't have been funded the time to implement lock-releasing code on every page, but that's a good strategy to deal with people browsing without javascript (I'd probably run code on the server to release locks on each page requested instead of doing it via javascript load).
Ty W