##// END OF EJS Templates
js: add failure callbacks to ajax functions to allow custom...
dan -
r1140:4a2f36ea default
parent child Browse files
Show More
@@ -1,62 +1,70 b''
1 // # Copyright (C) 2010-2016 RhodeCode GmbH
1 // # Copyright (C) 2010-2016 RhodeCode GmbH
2 // #
2 // #
3 // # This program is free software: you can redistribute it and/or modify
3 // # This program is free software: you can redistribute it and/or modify
4 // # it under the terms of the GNU Affero General Public License, version 3
4 // # it under the terms of the GNU Affero General Public License, version 3
5 // # (only), as published by the Free Software Foundation.
5 // # (only), as published by the Free Software Foundation.
6 // #
6 // #
7 // # This program is distributed in the hope that it will be useful,
7 // # This program is distributed in the hope that it will be useful,
8 // # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 // # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // # GNU General Public License for more details.
10 // # GNU General Public License for more details.
11 // #
11 // #
12 // # You should have received a copy of the GNU Affero General Public License
12 // # You should have received a copy of the GNU Affero General Public License
13 // # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 // # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 // #
14 // #
15 // # This program is dual-licensed. If you wish to learn more about the
15 // # This program is dual-licensed. If you wish to learn more about the
16 // # RhodeCode Enterprise Edition, including its added features, Support services,
16 // # RhodeCode Enterprise Edition, including its added features, Support services,
17 // # and proprietary license terms, please see https://rhodecode.com/licenses/
17 // # and proprietary license terms, please see https://rhodecode.com/licenses/
18
18
19 /**
19 /**
20 * turns objects into GET query string
20 * turns objects into GET query string
21 */
21 */
22 var toQueryString = function(o) {
22 var toQueryString = function(o) {
23 if(typeof o === 'string') {
23 if(typeof o === 'string') {
24 return o;
24 return o;
25 }
25 }
26 if(typeof o !== 'object') {
26 if(typeof o !== 'object') {
27 return false;
27 return false;
28 }
28 }
29 var _p, _qs = [];
29 var _p, _qs = [];
30 for(_p in o) {
30 for(_p in o) {
31 _qs.push(encodeURIComponent(_p) + '=' + encodeURIComponent(o[_p]));
31 _qs.push(encodeURIComponent(_p) + '=' + encodeURIComponent(o[_p]));
32 }
32 }
33 return _qs.join('&');
33 return _qs.join('&');
34 };
34 };
35
35
36 /**
36 /**
37 * ajax call wrappers
37 * ajax call wrappers
38 */
38 */
39 var ajaxGET = function(url, success) {
39 var ajaxGET = function(url, success, failure) {
40 var sUrl = url;
40 var sUrl = url;
41 var request = $.ajax({url: sUrl, headers: {'X-PARTIAL-XHR': true}})
41 var request = $.ajax({url: sUrl, headers: {'X-PARTIAL-XHR': true}})
42 .done(function(data){
42 .done(function(data){
43 success(data);
43 success(data);
44 })
44 })
45 .fail(function(data, textStatus, xhr){
45 .fail(function(data, textStatus, xhr) {
46 alert("error processing request: " + textStatus);
46 if (failure) {
47 failure(data, textStatus, xhr);
48 } else {
49 alert("error processing request: " + textStatus);
50 }
47 });
51 });
48 return request;
52 return request;
49 };
53 };
50 var ajaxPOST = function(url,postData,success) {
54 var ajaxPOST = function(url, postData, success, failure) {
51 var sUrl = url;
55 var sUrl = url;
52 var postData = toQueryString(postData);
56 var postData = toQueryString(postData);
53 var request = $.ajax({type: 'POST', data: postData, url: sUrl,
57 var request = $.ajax({type: 'POST', data: postData, url: sUrl,
54 headers: {'X-PARTIAL-XHR': true}})
58 headers: {'X-PARTIAL-XHR': true}})
55 .done(function(data){
59 .done(function(data){
56 success(data);
60 success(data);
57 })
61 })
58 .fail(function(data, textStatus, xhr){
62 .fail(function(data, textStatus, xhr){
59 alert("error processing request: " + textStatus);
63 if (failure) {
64 failure(data, textStatus, xhr);
65 } else {
66 alert("error processing request: " + textStatus);
67 }
60 });
68 });
61 return request;
69 return request;
62 };
70 };
General Comments 0
You need to be logged in to leave comments. Login now