Pleets /
SQLWebManager
| 1 | View Code Duplication | $(function(){ |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 2 | |||
| 3 | /** |
||
| 4 | * Submit forms with AJAX |
||
| 5 | */ |
||
| 6 | $("body").delegate("form[data-role='ajax-request']", "submit", function(event) |
||
| 7 | { |
||
| 8 | event.preventDefault(); |
||
| 9 | |||
| 10 | var formObject = $(this); |
||
| 11 | |||
| 12 | formObject.find("input").attr("readonly", "readonly"); |
||
| 13 | formObject.find("select").attr("readonly", "readonly"); |
||
| 14 | formObject.find("button[type='submit']").attr("disabled", "disabled"); |
||
| 15 | |||
| 16 | var url = $(this).attr('action'); |
||
| 17 | var type = $(this).attr('method'); |
||
| 18 | var box = $(this).attr('data-response'); |
||
| 19 | var data = $(this).attr('data-object'); |
||
| 20 | |||
| 21 | var call = eval($(this).attr('data-callback')) || {}; |
||
|
0 ignored issues
–
show
|
|||
| 22 | |||
| 23 | call.complete = call.complete || new Function(); |
||
|
0 ignored issues
–
show
|
|||
| 24 | call.success = call.success || new Function(); |
||
|
0 ignored issues
–
show
|
|||
| 25 | call.before = call.before || new Function(); |
||
|
0 ignored issues
–
show
|
|||
| 26 | call.error = call.error || new Function(); |
||
|
0 ignored issues
–
show
|
|||
| 27 | |||
| 28 | var form_data = $(this).serializeArray(); |
||
| 29 | |||
| 30 | var parsed = eval(data); |
||
|
0 ignored issues
–
show
|
|||
| 31 | |||
| 32 | for (var i in parsed) |
||
|
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty.
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
| 33 | { |
||
| 34 | form_data.push({ name: i, value: parsed[i] }); |
||
| 35 | } |
||
| 36 | |||
| 37 | $.ajax({ |
||
| 38 | url: url, |
||
| 39 | type: type, |
||
| 40 | data: form_data, |
||
| 41 | beforeSend: function() { |
||
| 42 | var loader = "<div class='loader-container'><div class='loader'><span></span><span></span><span></span><span></span><span></span></div></div>"; |
||
| 43 | $(box).html(loader); |
||
| 44 | call.before(); |
||
| 45 | }, |
||
| 46 | error: function(jqXHR, textStatus, errorThrown) |
||
| 47 | { |
||
| 48 | $(box).html("Error processing request!.<br />"); |
||
| 49 | |||
| 50 | var e = {}; |
||
| 51 | e.jqXHR = jqXHR; |
||
| 52 | console.info(jqXHR); |
||
| 53 | e.textStatus = textStatus; |
||
| 54 | e.errorThrown = errorThrown; |
||
| 55 | |||
| 56 | call.error(e); |
||
| 57 | |||
| 58 | $(box).append("Error <strong>" + jqXHR.status + "</strong> - " + jqXHR.statusText + "<br />"); |
||
| 59 | $(box).append("Status: " + textStatus + "<br />"); |
||
| 60 | $(box).append("readyState: " + jqXHR.readyState + "<br />"); |
||
| 61 | $(box).append(jqXHR.responseText + "<br /><br />"); |
||
| 62 | }, |
||
| 63 | success: function(data) |
||
| 64 | { |
||
| 65 | $(box).html(data); |
||
| 66 | call.success(data); |
||
| 67 | }, |
||
| 68 | complete: function(data) |
||
| 69 | { |
||
| 70 | formObject.find("input").removeAttr("readonly"); |
||
| 71 | formObject.find("select").removeAttr("readonly"); |
||
| 72 | formObject.find("button[type='submit']").removeAttr("disabled"); |
||
| 73 | call.success(data); |
||
| 74 | } |
||
| 75 | }); |
||
| 76 | }); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * General AJAX request |
||
| 80 | */ |
||
| 81 | $("body").delegate(":not(form)[data-role='ajax-request']", "click", function(event) |
||
| 82 | { |
||
| 83 | var url = $(this).attr('data-url'); |
||
| 84 | var type = $(this).attr('data-type'); |
||
| 85 | var box = $(this).attr('data-response'); |
||
| 86 | var data = $(this).attr('data-object'); |
||
| 87 | var frm = $(this).attr('data-form'); |
||
| 88 | |||
| 89 | var call = eval($(this).attr('data-callback')) || {}; |
||
|
0 ignored issues
–
show
|
|||
| 90 | |||
| 91 | call.complete = call.complete || new Function(); |
||
|
0 ignored issues
–
show
|
|||
| 92 | call.success = call.success || new Function(); |
||
|
0 ignored issues
–
show
|
|||
| 93 | call.before = call.before || new Function(); |
||
|
0 ignored issues
–
show
|
|||
| 94 | call.error = call.error || new Function(); |
||
|
0 ignored issues
–
show
|
|||
| 95 | |||
| 96 | var form_data = $(frm).serializeArray(); |
||
| 97 | |||
| 98 | var parsed = eval(data); |
||
|
0 ignored issues
–
show
|
|||
| 99 | |||
| 100 | for (var i in parsed) |
||
|
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty.
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
| 101 | { |
||
| 102 | form_data.push({ name: i, value: parsed[i] }); |
||
| 103 | } |
||
| 104 | |||
| 105 | $.ajax({ |
||
| 106 | url: url, |
||
| 107 | type: type, |
||
| 108 | data: form_data, |
||
| 109 | beforeSend: function() { |
||
| 110 | var loader = "<div class='loader-container'><div class='loader'><span></span><span></span><span></span><span></span><span></span></div></div>"; |
||
| 111 | $(box).html(loader); |
||
| 112 | call.before(); |
||
| 113 | }, |
||
| 114 | error: function(jqXHR, textStatus, errorThrown) |
||
| 115 | { |
||
| 116 | event.preventDefault(); |
||
| 117 | |||
| 118 | $(box).html("Error processing request!. " + errorThrown); |
||
| 119 | |||
| 120 | var e = {}; |
||
| 121 | e.jqXHR = jqXHR; |
||
| 122 | e.textStatus = textStatus; |
||
| 123 | e.errorThrown = errorThrown; |
||
| 124 | |||
| 125 | call.error(e); |
||
| 126 | }, |
||
| 127 | success: function(data) |
||
| 128 | { |
||
| 129 | $(box).html(data); |
||
| 130 | call.success(data); |
||
| 131 | }, |
||
| 132 | complete: function() |
||
| 133 | { |
||
| 134 | call.complete(); |
||
| 135 | } |
||
| 136 | }); |
||
| 137 | }); |
||
| 138 | }); |