Conditions | 1 |
Paths | 2 |
Total Lines | 82 |
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 ($, app) { |
||
2 | 'use strict'; |
||
3 | |||
4 | $.extend(app, { |
||
5 | PJAX_FLASH_CONTAINER: '.notification-container', |
||
6 | PJAX_FLASH: 'pjax-flashes', |
||
7 | PJAX_FLASH_SELECTOR: '[data-pjax-flashes]', |
||
8 | |||
9 | MESSAGE_SUCCESS: 0, |
||
10 | MESSAGE_ERROR: 1, |
||
11 | MESSAGE_WARNING: 2, |
||
12 | MESSAGE_NOTICE: 3, |
||
13 | |||
14 | message: function (text, status) { |
||
15 | this.clearMessage(); |
||
16 | if (!status) { |
||
17 | status = this.MESSAGE_SUCCESS; |
||
18 | } |
||
19 | |||
20 | var msgbox = $('<div></div>').appendTo(app.PJAX_FLASH_CONTAINER); |
||
21 | msgbox.addClass('alert'); |
||
22 | msgbox.hide(); |
||
23 | msgbox.text(text); |
||
24 | switch (status) { |
||
|
|||
25 | case this.MESSAGE_SUCCESS : |
||
26 | case 'success' : |
||
27 | msgbox.addClass('alert-success'); |
||
28 | break; |
||
29 | case this.MESSAGE_NOTICE : |
||
30 | case 'notice' : |
||
31 | msgbox.addClass('alert-info'); |
||
32 | break; |
||
33 | case this.MESSAGE_WARNING : |
||
34 | msgbox.addClass('alert-warning'); |
||
35 | msgbox.html('<i class="icon-warning-sign"></i> ' + msgbox.text()); |
||
36 | break; |
||
37 | case this.MESSAGE_ERROR : |
||
38 | case 'error' : |
||
39 | msgbox.addClass('alert-danger'); |
||
40 | msgbox.html('<i class="icon-erroralt"></i> ' + msgbox.text()); |
||
41 | break; |
||
42 | } |
||
43 | |||
44 | msgbox.data("time", new Date()); |
||
45 | msgbox.fadeIn(500); |
||
46 | |||
47 | }, |
||
48 | |||
49 | clearMessage: function () { |
||
50 | $(app.PJAX_FLASH_CONTAINER + ' > div').each(function () { |
||
51 | var time = $(this).data("time"); |
||
52 | if (!time || (new Date() - time >= 5 * 1000)) { |
||
53 | $(this).fadeOut(1000, function () { |
||
54 | $(this).html(""); |
||
55 | }); |
||
56 | } |
||
57 | }); |
||
58 | } |
||
59 | }); |
||
60 | |||
61 | $(document) |
||
62 | .on('pjax:send', app.clearMessage) |
||
63 | .on('pjax:error', function (event, xhr, textStatus, error, options) { |
||
64 | if ('abort' !== textStatus) { |
||
65 | app.message('Error', app.MESSAGE_ERROR); |
||
66 | } |
||
67 | }) |
||
68 | .on('pjax:beforeReplace', function (event, contents, options) { |
||
69 | var tmp = $('<div></div>'); |
||
70 | tmp.html(contents); |
||
71 | var flashes = $(app.PJAX_FLASH_SELECTOR, tmp); |
||
72 | if (flashes) { |
||
73 | var flashData = flashes.data(app.PJAX_FLASH); |
||
74 | if (flashData) { |
||
75 | $(app.PJAX_FLASH_CONTAINER).html(flashData); |
||
76 | } |
||
77 | flashes.removeData(app.PJAX_FLASH); |
||
78 | flashes.removeAttr('data-' + app.PJAX_FLASH); |
||
79 | } |
||
80 | }); |
||
81 | |||
82 | })(jQuery, application); |
||
83 |