| Conditions | 9 |
| Total Lines | 51 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 | // TODO: Refactor the heck out of this mess |
||
| 138 | function addAllWanders(map) |
||
| 139 | { |
||
| 140 | // TODO: We should probably use some kind of Hydra client. This"ll do for now. |
||
| 141 | $.getJSON("/api/wanders", function(data) { |
||
| 142 | var last = data["hydra:totalItems"]; |
||
| 143 | var layers = []; |
||
| 144 | $.each(data["hydra:member"], function(key, wander) { |
||
| 145 | var isLastWander = (last - 1 === key); |
||
| 146 | var track = omnivore.gpx(wander.gpxFilename, |
||
| 147 | null, |
||
| 148 | new CustomGeoJSON(null, { |
||
| 149 | wanderId: wander.id, |
||
| 150 | style: isLastWander ? selectedWanderStyle() : unselectedWanderStyle() |
||
| 151 | })) |
||
| 152 | .bindPopup(function(layer) { |
||
| 153 | // Toggle styles |
||
| 154 | currentlySelected.setStyle(unselectedWanderStyle()); |
||
| 155 | layer.setStyle(selectedWanderStyle()); |
||
| 156 | layer.bringToFront(); |
||
| 157 | currentlySelected = layer; |
||
| 158 | |||
| 159 | addWanderImages(map, layer.options.wanderId); |
||
| 160 | // Popup |
||
| 161 | var template = "<a href='{contentUrl}'>{title}</a>"; |
||
| 162 | return L.Util.template(template, wander); |
||
| 163 | }); |
||
| 164 | track.wanderId = wander.id; |
||
| 165 | track.addTo(map); |
||
| 166 | layers.push(track); |
||
| 167 | if (isLastWander) { |
||
| 168 | currentlySelected = track; |
||
| 169 | track.on("ready", function() { |
||
| 170 | track.bringToFront(); |
||
| 171 | }); |
||
| 172 | } else { |
||
| 173 | track.on("ready", function() { |
||
| 174 | // Our layers load asynchonously, and I can't find an event |
||
| 175 | // that fires once all our geoJSON layers are loaded, so the |
||
| 176 | // above bringToFront() for the most recent wander might |
||
| 177 | // fire before other layers load. This is a bit of a hack to |
||
| 178 | // send any layers that load later to the back, keeping the |
||
| 179 | // most recent wander at the front. |
||
| 180 | track.bringToBack(); |
||
| 181 | }); |
||
| 182 | } |
||
| 183 | }); |
||
| 184 | $.when.apply($, layers).then(function() { |
||
| 185 | console.log('All loaded'); |
||
|
|
|||
| 186 | }); |
||
| 187 | }); |
||
| 188 | } |
||
| 189 | |||
| 204 |