Conditions | 1 |
Paths | 1024 |
Total Lines | 134 |
Code Lines | 83 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | $(function(){ |
||
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.addClass('loading'); |
||
13 | formObject.find("input").attr("readonly", "readonly"); |
||
14 | formObject.find("select").attr("readonly", "readonly"); |
||
15 | formObject.find("button[type='submit']").attr("disabled", "disabled"); |
||
16 | |||
17 | var url = $(this).attr('action'); |
||
18 | var type = $(this).attr('method'); |
||
19 | var box = $(this).attr('data-response'); |
||
20 | var data = $(this).attr('data-object'); |
||
21 | |||
22 | var call = eval($(this).attr('data-callback')) || {}; |
||
|
|||
23 | |||
24 | call.complete = call.complete || new Function(); |
||
25 | call.success = call.success || new Function(); |
||
26 | call.before = call.before || new Function(); |
||
27 | call.error = call.error || new Function(); |
||
28 | |||
29 | var form_data = $(this).serializeArray(); |
||
30 | |||
31 | var parsed = eval(data); |
||
32 | |||
33 | for (var i in parsed) |
||
34 | { |
||
35 | form_data.push({ name: i, value: parsed[i] }); |
||
36 | } |
||
37 | |||
38 | $.ajax({ |
||
39 | url: url, |
||
40 | type: type, |
||
41 | data: form_data, |
||
42 | beforeSend: function() { |
||
43 | var loader = "<div class='ui active inline loader'></div>"; |
||
44 | $(box).html(loader); |
||
45 | call.before(); |
||
46 | }, |
||
47 | error: function(jqXHR, textStatus, errorThrown) |
||
48 | { |
||
49 | $(box).html("Error processing request!. " + errorThrown); |
||
50 | |||
51 | var e = {}; |
||
52 | e.jqXHR = jqXHR; |
||
53 | e.textStatus = textStatus; |
||
54 | e.errorThrown = errorThrown; |
||
55 | |||
56 | call.error(e); |
||
57 | }, |
||
58 | success: function(data) |
||
59 | { |
||
60 | $(box).html(data); |
||
61 | call.success(data); |
||
62 | }, |
||
63 | complete: function(data) |
||
64 | { |
||
65 | formObject.find("input").removeAttr("readonly"); |
||
66 | formObject.find("select").removeAttr("readonly"); |
||
67 | formObject.find("button[type='submit']").removeAttr("disabled"); |
||
68 | formObject.removeClass('loading'); |
||
69 | call.success(data); |
||
70 | } |
||
71 | }); |
||
72 | }); |
||
73 | |||
74 | /** |
||
75 | * General AJAX request |
||
76 | */ |
||
77 | $("body").delegate(":not(form)[data-role='ajax-request']", "click", function(event) |
||
78 | { |
||
79 | event.preventDefault(); |
||
80 | |||
81 | var url = $(this).attr('data-url'); |
||
82 | var type = $(this).attr('data-type'); |
||
83 | var box = $(this).attr('data-response'); |
||
84 | var data = $(this).attr('data-object'); |
||
85 | var frm = $(this).attr('data-form'); |
||
86 | |||
87 | var call = eval($(this).attr('data-callback')) || {}; |
||
88 | |||
89 | call.complete = call.complete || new Function(); |
||
90 | call.success = call.success || new Function(); |
||
91 | call.before = call.before || new Function(); |
||
92 | call.error = call.error || new Function(); |
||
93 | |||
94 | var form_data = $(frm).serializeArray(); |
||
95 | |||
96 | var parsed = eval(data); |
||
97 | |||
98 | for (var i in parsed) |
||
99 | { |
||
100 | form_data.push({ name: i, value: parsed[i] }); |
||
101 | } |
||
102 | |||
103 | $.ajax({ |
||
104 | url: url, |
||
105 | type: type, |
||
106 | data: form_data, |
||
107 | beforeSend: function() { |
||
108 | var loader = "<div class='ui active inline loader'></div>"; |
||
109 | $(box).html(loader); |
||
110 | call.before(); |
||
111 | }, |
||
112 | error: function(jqXHR, textStatus, errorThrown) |
||
113 | { |
||
114 | $(box).html("Error processing request!. " + errorThrown); |
||
115 | |||
116 | var e = {}; |
||
117 | e.jqXHR = jqXHR; |
||
118 | e.textStatus = textStatus; |
||
119 | e.errorThrown = errorThrown; |
||
120 | |||
121 | call.error(e); |
||
122 | }, |
||
123 | success: function(data) |
||
124 | { |
||
125 | $(box).html(data); |
||
126 | call.success(data); |
||
127 | }, |
||
128 | complete: function() |
||
129 | { |
||
130 | call.complete(); |
||
131 | } |
||
132 | }); |
||
133 | }); |
||
134 | }); |