Conditions | 14 |
Paths | 7 |
Total Lines | 133 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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:
Complex classes like usersList._add often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /*global document, confirm, $ */ |
||
283 | _add: function() { |
||
284 | this._alert.classList.add('hidden') |
||
285 | var username = document.querySelector('[data-add-user-username]') |
||
286 | if ( |
||
287 | typeof username.value === 'undefined' || |
||
288 | username.value === null || |
||
289 | username.value === '' |
||
290 | ) { |
||
291 | username.parentNode.classList.add('has-error') |
||
292 | return |
||
293 | } |
||
294 | username.parentNode.classList.remove('has-error') |
||
295 | |||
296 | var name = document.querySelector('[data-add-user-name]') |
||
297 | if ( |
||
298 | typeof name.value === 'undefined' || |
||
299 | name.value === null || |
||
300 | name.value === '' |
||
301 | ) { |
||
302 | name.parentNode.classList.add('has-error') |
||
303 | return |
||
304 | } |
||
305 | name.parentNode.classList.remove('has-error') |
||
306 | |||
307 | var email = document.querySelector('[data-add-user-email]') |
||
308 | if ( |
||
309 | typeof email.value === 'undefined' || |
||
310 | email.value === null || |
||
311 | email.value === '' |
||
312 | ) { |
||
313 | email.parentNode.classList.add('has-error') |
||
314 | return |
||
315 | } |
||
316 | if (!this._validateEmail(email.value)) { |
||
317 | email.parentNode.classList.add('has-error') |
||
318 | this._alert.classList.remove('hidden') |
||
319 | this._alert.innerHTML = 'email is invalid' |
||
320 | return |
||
321 | } |
||
322 | email.parentNode.classList.remove('has-error') |
||
323 | |||
324 | var password = document.querySelector('[data-add-user-password]') |
||
325 | if ( |
||
326 | typeof password.value === 'undefined' || |
||
327 | password.value === null || |
||
328 | password.value === '' |
||
329 | ) { |
||
330 | password.parentNode.classList.add('has-error') |
||
331 | return |
||
332 | } |
||
333 | |||
334 | password.parentNode.classList.remove('has-error') |
||
335 | |||
336 | var role = document.querySelector('[data-add-user-role]') |
||
337 | var toSave = qs.stringify({ |
||
338 | username: username.value, |
||
339 | name: name.value, |
||
340 | email: email.value, |
||
341 | password: password.value, |
||
342 | role: role.value |
||
343 | }) |
||
344 | |||
345 | this._ajax( |
||
346 | { |
||
347 | url: '/abe/users/add', |
||
348 | body: toSave, |
||
349 | method: 'post' |
||
350 | }, |
||
351 | (code, responseText) => { |
||
352 | var data = JSON.parse(responseText) |
||
353 | if (data.success === 1) { |
||
354 | var tr = document.createElement('tr') |
||
355 | var oldTr = document.querySelector('[data-user-base]') |
||
356 | if (typeof oldTr !== 'undefined' && oldTr !== null) { |
||
357 | tr.innerHTML = oldTr.innerHTML |
||
358 | |||
359 | var tdUsername = tr.querySelector('.td-username') |
||
360 | tdUsername.querySelector('.value').innerHTML = data.user.username |
||
361 | tdUsername.querySelector('input').value = data.user.username |
||
362 | |||
363 | var tdName = tr.querySelector('.td-name') |
||
364 | tdName.querySelector('.value').innerHTML = data.user.name |
||
365 | tdName.querySelector('input').value = data.user.name |
||
366 | |||
367 | var tdEmail = tr.querySelector('.td-email') |
||
368 | tdEmail.querySelector('.value').innerHTML = data.user.email |
||
369 | tdEmail.querySelector('input').value = data.user.email |
||
370 | |||
371 | var tdRole = tr.querySelector('.td-role') |
||
372 | tdRole.querySelector('.value').innerHTML = data.user.role.name |
||
373 | var tdRoleOptions = tdRole.querySelectorAll('select option') |
||
374 | Array.prototype.forEach.call(tdRoleOptions, function(option) { |
||
375 | if (option.value === data.user.role.name) { |
||
376 | option.selected = 'selected' |
||
377 | } |
||
378 | }) |
||
379 | |||
380 | var tdActive = tr.querySelector('.td-active') |
||
381 | var glypEyeClose = tdActive.querySelector('.glyphicon-eye-close') |
||
382 | glypEyeClose.addEventListener('click', this._handleActivate, true) |
||
383 | glypEyeClose.setAttribute('data-user-id', data.user.id) |
||
384 | |||
385 | var tdActions = tr.querySelector('.td-actions') |
||
386 | var glypEdit = tdActions.querySelector('.glyphicon-pencil') |
||
387 | glypEdit.addEventListener('click', this._handleEdit, true) |
||
388 | glypEdit.setAttribute('data-user-id', data.user.id) |
||
389 | |||
390 | var glypOk = tdActions.querySelector('.glyphicon-ok') |
||
391 | glypOk.addEventListener('click', this._handleUpdate, true) |
||
392 | glypOk.setAttribute('data-user-id', data.user.id) |
||
393 | |||
394 | var glypCloseUpdate = tdActions.querySelector('.glyphicon-remove') |
||
395 | glypCloseUpdate.setAttribute('data-user-id', data.user.id) |
||
396 | // glypCloseUpdate.addEventListener('click', this._handleCloseUpdate, true) |
||
397 | |||
398 | var glypRemove = tdActions.querySelector('.glyphicon-trash') |
||
399 | glypRemove.setAttribute('data-user-id', data.user.id) |
||
400 | glypRemove.addEventListener('click', this._handleRemove, true) |
||
401 | } |
||
402 | |||
403 | this._table.appendChild(tr) |
||
404 | |||
405 | username.value = '' |
||
406 | name.value = '' |
||
407 | email.value = '' |
||
408 | password.value = '' |
||
409 | } else { |
||
410 | this._alert.classList.remove('hidden') |
||
411 | this._alert.innerHTML = data.message |
||
412 | } |
||
413 | } |
||
414 | ) |
||
415 | } |
||
416 | } |
||
424 |