| Conditions | 1 |
| Paths | 288 |
| Total Lines | 93 |
| 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 | /** global: GLSR, XMLHttpRequest */ |
||
| 2 | ;(function() { |
||
| 3 | |||
| 4 | 'use strict'; |
||
| 5 | |||
| 6 | var Ajax = function() { |
||
| 7 | this.get = this.get_; |
||
| 8 | this.isFileAPISupported = this.isFileAPISupported_; |
||
| 9 | this.isFormDataSupported = this.isFormDataSupported_; |
||
| 10 | this.isUploadSupported = this.isUploadSupported_; |
||
| 11 | this.post = this.post_; |
||
| 12 | }; |
||
| 13 | |||
| 14 | Ajax.prototype = { |
||
| 15 | /** @return void */ |
||
| 16 | get_: function( url, successCallback, headers ) { |
||
| 17 | this.xhr = new XMLHttpRequest(); |
||
| 18 | this.xhr.open( 'GET', url ); |
||
| 19 | this.xhr.onreadystatechange = function() { |
||
| 20 | if( this.xhr.readyState !== 4 || this.xhr.status !== 200 )return; |
||
| 21 | successCallback( this.xhr.responseText ); |
||
| 22 | }.bind( this ); |
||
| 23 | this.setHeaders_( headers ); |
||
| 24 | this.xhr.send(); |
||
| 25 | }, |
||
| 26 | |||
| 27 | /** @return bool */ |
||
| 28 | isFileAPISupported_: function() { |
||
| 29 | var input = document.createElement( 'INPUT' ); |
||
| 30 | input.type = 'file'; |
||
| 31 | return 'files' in input; |
||
| 32 | }, |
||
| 33 | |||
| 34 | /** @return bool */ |
||
| 35 | isFormDataSupported_: function() { |
||
| 36 | return !!window.FormData; |
||
| 37 | }, |
||
| 38 | |||
| 39 | /** @return bool */ |
||
| 40 | isUploadSupported_: function() { |
||
| 41 | var xhr = new XMLHttpRequest(); |
||
| 42 | return !!( xhr && ( 'upload' in xhr ) && ( 'onprogress' in xhr.upload )); |
||
| 43 | }, |
||
| 44 | |||
| 45 | /** @return FormData */ |
||
| 46 | buildFormData_: function( formData, data, parentKey ) { |
||
| 47 | if( typeof data !== 'object' || data instanceof Date || data instanceof File ) { |
||
|
|
|||
| 48 | formData.append( parentKey, data || '' ); |
||
| 49 | } |
||
| 50 | else { |
||
| 51 | Object.keys( data ).forEach( function( key ) { |
||
| 52 | if( !data.hasOwnProperty( key ))return; |
||
| 53 | formData = this.buildFormData_( formData, data[key], parentKey ? parentKey[key] : key ); |
||
| 54 | }.bind( this )); |
||
| 55 | } |
||
| 56 | return formData; |
||
| 57 | }, |
||
| 58 | |||
| 59 | /** @return FormData */ |
||
| 60 | normalizeData_: function( data ) { // object |
||
| 61 | var formData = new FormData( data ); |
||
| 62 | if( Object.prototype.toString.call( data ) !== '[object HTMLFormElement]' ) { |
||
| 63 | formData = this.buildFormData_( formData, data ); |
||
| 64 | } |
||
| 65 | formData.append( 'action', GLSR.action ); |
||
| 66 | formData.append( 'ajax_request', true ); |
||
| 67 | return formData; |
||
| 68 | }, |
||
| 69 | |||
| 70 | /** @return void */ |
||
| 71 | post_: function( formOrData, successCallback, headers ) { |
||
| 72 | this.xhr = new XMLHttpRequest(); |
||
| 73 | this.xhr.open( 'POST', GLSR.ajaxurl ); |
||
| 74 | this.setHeaders_( headers ); |
||
| 75 | this.xhr.send( this.normalizeData_( formOrData )); |
||
| 76 | this.xhr.onreadystatechange = function() { |
||
| 77 | if( this.xhr.readyState !== XMLHttpRequest.DONE || this.xhr.status !== 200 )return; |
||
| 78 | successCallback( JSON.parse( this.xhr.responseText )); |
||
| 79 | }.bind( this ); |
||
| 80 | }, |
||
| 81 | |||
| 82 | /** @return void */ |
||
| 83 | setHeaders_: function( headers ) { |
||
| 84 | headers = headers || {}; |
||
| 85 | headers['X-Requested-With'] = 'XMLHttpRequest'; |
||
| 86 | for( var key in headers ) { |
||
| 87 | if( !headers.hasOwnProperty( key ))continue; |
||
| 88 | this.xhr.setRequestHeader( key, headers[key] ); |
||
| 89 | } |
||
| 90 | }, |
||
| 91 | }; |
||
| 92 | |||
| 93 | GLSR.Ajax = Ajax; |
||
| 94 | })(); |
||
| 95 |
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.