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()
});
}
});