When a user requests editing an entry in our CMS, we “lock” it so that no one else can edit it at the same time, we release the lock when they submit their changes. However, we need to handle the case when the user leaves the page through other links ... My first attempt is to use jQuery to start a synchronous call $.ajax()to $(window).unload. This works, however, on the next page that the user sees, the entry appears to be locked (provided that they return to the page with a list of all entries, perhaps using the "Back" button). A simple update of this page shows that the record is ready for editing again.
I assume that for some reason the browser will receive the next page before the ajax request is fully processed. The question is, is there a way to ensure that things happen in the correct order.
var fire_unload_ajax = true;
$(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()
});
}
});
source
share