MDL-58310 ajax: suppress errors upon page unload

This commit is contained in:
John Okely 2017-05-29 08:30:24 +08:00
parent 46f68f026e
commit 671be2bedb
2 changed files with 14 additions and 3 deletions

View File

@ -1 +1 @@
define(["jquery","core/config"],function(a,b){var c=function(a){var b,c,d=this,e=null,f=0;for(f=0;f<d.length;f++){if(b=d[f],c=a[f],"undefined"==typeof c){e=new Error("missing response");break}if(c.error!==!1){e=c.exception;break}b.deferred.resolve(c.data)}if(null!==e)for(;f<d.length;f++)b=d[f],b.deferred.reject(e)},d=function(a,b){var c=this,d=0;for(d=0;d<c.length;d++){var e=c[d];e.deferred.reject(b)}};return{call:function(e,f,g){var h,i=[],j=[],k=[],l="";for("undefined"==typeof g&&(g=!0),"undefined"==typeof f&&(f=!0),h=0;h<e.length;h++){var m=e[h];i.push({index:h,methodname:m.methodname,args:m.args}),m.deferred=a.Deferred(),j.push(m.deferred.promise()),"undefined"!=typeof m.done&&m.deferred.done(m.done),"undefined"!=typeof m.fail&&m.deferred.fail(m.fail),m.index=h,k.push(m.methodname)}l=k.length<=5?k.sort().join():k.length+"-method-calls",i=JSON.stringify(i);var n={type:"POST",data:i,context:e,dataType:"json",processData:!1,async:f,contentType:"application/json"},o="service.php";g||(o="service-nologin.php");var p=b.wwwroot+"/lib/ajax/"+o+"?sesskey="+b.sesskey+"&info="+l;return f?a.ajax(p,n).done(c).fail(d):(n.success=c,n.error=d,a.ajax(p,n)),j}}});
define(["jquery","core/config","core/log"],function(a,b,c){var d=!1,e=function(a){var b,c,d=this,e=null,f=0;for(f=0;f<d.length;f++){if(b=d[f],c=a[f],"undefined"==typeof c){e=new Error("missing response");break}if(c.error!==!1){e=c.exception;break}b.deferred.resolve(c.data)}if(null!==e)for(;f<d.length;f++)b=d[f],b.deferred.reject(e)},f=function(a,b){var e=this,f=0;for(f=0;f<e.length;f++){var g=e[f];d?c.error("Page unload: "+b):g.deferred.reject(b)}};return{call:function(c,g,h){a(window).bind("beforeunload",function(){d=!0});var i,j=[],k=[],l=[],m="";for("undefined"==typeof h&&(h=!0),"undefined"==typeof g&&(g=!0),i=0;i<c.length;i++){var n=c[i];j.push({index:i,methodname:n.methodname,args:n.args}),n.deferred=a.Deferred(),k.push(n.deferred.promise()),"undefined"!=typeof n.done&&n.deferred.done(n.done),"undefined"!=typeof n.fail&&n.deferred.fail(n.fail),n.index=i,l.push(n.methodname)}m=l.length<=5?l.sort().join():l.length+"-method-calls",j=JSON.stringify(j);var o={type:"POST",data:j,context:c,dataType:"json",processData:!1,async:g,contentType:"application/json"},p="service.php";h||(p="service-nologin.php");var q=b.wwwroot+"/lib/ajax/"+p+"?sesskey="+b.sesskey+"&info="+m;return g?a.ajax(q,o).done(e).fail(f):(o.success=e,o.error=f,a.ajax(q,o)),k}}});

View File

@ -25,7 +25,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 2.9
*/
define(['jquery', 'core/config'], function($, config) {
define(['jquery', 'core/config', 'core/log'], function($, config, Log) {
// Keeps track of when the user leaves the page so we know not to show an error.
var unloading = false;
/**
* Success handler. Called when the ajax call succeeds. Checks each response and
@ -87,7 +90,12 @@ define(['jquery', 'core/config'], function($, config) {
for (i = 0; i < requests.length; i++) {
var request = requests[i];
request.deferred.reject(textStatus);
if (unloading) {
// No need to trigger an error because we are already navigating.
Log.error("Page unload: " + textStatus);
} else {
request.deferred.reject(textStatus);
}
}
};
@ -109,6 +117,9 @@ define(['jquery', 'core/config'], function($, config) {
* @return {Promise[]} Array of promises that will be resolved when the ajax call returns.
*/
call: function(requests, async, loginrequired) {
$(window).bind('beforeunload', function() {
unloading = true;
});
var ajaxRequestData = [],
i,
promises = [],