model.Model.save() operation doesn't complete on beforeUnload
When adding a model.Model.save() function to window.unload, the save() does not complete on the server side. I tested the code in the console outside of the unload function, and the save completes as expected.
I noticed that the jQuery deferred promise object returned by the mode save function remains in a 'pending' state forever. Why would this be?
Is there a fix?
Code is below if you want to reproduce the issue.
I noticed that the jQuery deferred promise object returned by the mode save function remains in a 'pending' state forever. Why would this be?
Is there a fix?
Code is below if you want to reproduce the issue.
(function(skuid){ var $ = skuid.$;
$(document.body).one('pageload',function(){
$(window).unload(function(){
try{
removeUnfinishedRows();
}
catch(e){
console.log(e);
}
debugger;
});
});
})(skuid);
function removeUnfinishedRows(){
var rowsToCheck = skuid.model.getModel('SubmissionsForSelectedBanks');
var rowsToCheckData = rowsToCheck.data;
//cancel changed since save() needs to be called in order to delete rows.
//if save is called and changes aren't cancelled, some unintended changes may occur.
rowsToCheck.cancel();
//iterate thru each submission in the model that creates submissions and make sure that they've been sent
//if they haven't been sent, mark them for deletion
rowsToCheckData.forEach(
function(element,index,array){
rowToCheckTemp = rowsToCheckData[index];
if(!rowToCheckTemp.Custom_Relationship__r.Form_Is_Incomplete__c){
rowsToCheck.deleteRow({ Id: rowToCheckTemp.Id });
}
}
);
savePromise = rowsToCheck.save();
console.log(savePromise); //this prints 'pending'
//only uncomment if you want your tab to crash. This while loop goes on forever
/*while(savePromise.state()=='pending'){
//do nothing... wait until promise is done failing or resolving
}*/
}
1
Categories
- 7.9K Questions
- 926 Ideas
- 220 Discussion Categories
- 178 General
- 9 Community Feedback
- 3 Community Info
- 18 Knowledge Base
Comments
$window.on('beforeunload',function(){})
http://stackoverflow.com/questions/6895564/difference-between-onbeforeunload-and-onunload