Conditions | 1 |
Paths | 1 |
Total Lines | 141 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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(controller) { |
||
2 | |||
3 | 'use strict'; |
||
4 | |||
5 | /** @namespace faulancer.routing */ |
||
6 | var ns = faulancer.namespace('docs.routing'); |
||
|
|||
7 | |||
8 | var _fields = { |
||
9 | paths: [], |
||
10 | basePath: 'faulancer', |
||
11 | currentPath: '/' |
||
12 | }; |
||
13 | |||
14 | var _private = { |
||
15 | |||
16 | /** |
||
17 | * Get the action |
||
18 | * |
||
19 | * @param {String} path |
||
20 | * @returns {Function} |
||
21 | */ |
||
22 | getAction: function(path) { |
||
23 | |||
24 | if (controller.hasOwnProperty(path + 'Action')) { |
||
25 | |||
26 | var ctrl = 'controller'; |
||
27 | var method = path + 'Action'; |
||
28 | var target = faulancer['docs'][ctrl][method]; |
||
29 | |||
30 | if (typeof target === 'function') { |
||
31 | return target(); |
||
32 | } |
||
33 | |||
34 | } else { |
||
35 | |||
36 | return controller.errorAction(); |
||
37 | |||
38 | } |
||
39 | |||
40 | }, |
||
41 | |||
42 | /** |
||
43 | * Run dispatcher |
||
44 | * |
||
45 | * @param {String} path |
||
46 | * @returns {Function} |
||
47 | */ |
||
48 | run: function(path) { |
||
49 | |||
50 | if (path.substr(0,1) === '/') { |
||
51 | path = path.substr(1, path.length); |
||
52 | } |
||
53 | |||
54 | if (document.querySelector('a.selected')) { |
||
55 | document.querySelector('a.selected').classList.remove('selected'); |
||
56 | } |
||
57 | |||
58 | document.querySelector('[href="/' + path + '"]').classList.add('selected'); |
||
59 | |||
60 | |||
61 | return this.getAction(path); |
||
62 | |||
63 | |||
64 | |||
65 | } |
||
66 | |||
67 | }; |
||
68 | |||
69 | var _events = { |
||
70 | |||
71 | onDocumentLoad: function() { |
||
72 | _private.run('/documentation'); |
||
73 | }, |
||
74 | |||
75 | onLinkClick: function(e) { |
||
76 | e.preventDefault(); |
||
77 | _helpers.addPath(e.target); |
||
78 | _private.run(_helpers.getPath()); |
||
79 | _fields.currentPath = _helpers.getPath(); |
||
80 | return false; |
||
81 | } |
||
82 | |||
83 | }; |
||
84 | |||
85 | var _helpers = { |
||
86 | |||
87 | addPath: function(item) { |
||
88 | |||
89 | var path = item.getAttribute('href'); |
||
90 | window.history.pushState(null, 'Site', _fields.basePath + path); |
||
91 | |||
92 | }, |
||
93 | |||
94 | getPath: function() { |
||
95 | return window.location.pathname.replace(_fields.basePath, ''); |
||
96 | }, |
||
97 | |||
98 | detectPathChanges: function() { |
||
99 | |||
100 | if (_fields.currentPath !== _helpers.getPath()) { |
||
101 | |||
102 | _fields.currentPath = _helpers.getPath(); |
||
103 | _private.run(_helpers.getPath()); |
||
104 | |||
105 | } |
||
106 | |||
107 | } |
||
108 | |||
109 | }; |
||
110 | |||
111 | ns.dispatcher = { |
||
112 | |||
113 | init: function() { |
||
114 | |||
115 | _fields.basePath = document.querySelector('base').getAttribute('href'); |
||
116 | |||
117 | var links = document.querySelectorAll('a'); |
||
118 | |||
119 | for (var link in links) { |
||
120 | |||
121 | if (!links.hasOwnProperty(link)) { |
||
122 | continue; |
||
123 | } |
||
124 | |||
125 | links[link].addEventListener('click', _events.onLinkClick); |
||
126 | |||
127 | } |
||
128 | |||
129 | _events.onDocumentLoad(); |
||
130 | |||
131 | //setInterval(_helpers.detectPathChanges, 500); |
||
132 | |||
133 | } |
||
134 | |||
135 | }; |
||
136 | |||
137 | window.addEventListener('load', ns.dispatcher.init); |
||
138 | |||
139 | |||
140 | |||
141 | })(faulancer.namespace('docs.controller')); |
||
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.