Passed
Push — master ( 32d871...06bde6 )
by Paul
06:00
created

GLSR.Ajax.postFromEvent   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
1
/** global: GLSR, jQuery */
2
;(function( x ) {
3
4
	'use strict';
5
6
	GLSR.Ajax = {
7
		/** @return void */
8
		post: function( request, callback ) { // object, function|void
9
			var data = {
10
				action: GLSR.action,
11
				request: request,
12
			};
13
			x.post( GLSR.ajaxurl, data, function( response ) {
14
				if( typeof callback !== 'function' )return;
15
				callback( response );
16
			});
17
		},
18
19
		/** @return void */
20
		postFromEvent: function( ev, request, callback ) { // Event, object, function|void
21
			ev.preventDefault();
22
			var el = x( ev.target );
23
			if( el.is( ':disabled' ))return;
24
			request.nonce = request.nonce || el.closest( 'form' ).find( '#_wpnonce' ).val();
25
			var data = {
26
				action: GLSR.action,
27
				request: request,
28
			};
29
			el.prop( 'disabled', true );
30
			x.post( GLSR.ajaxurl, data, function( response ) {
31
				if( typeof callback === 'function' ) {
32
					callback( response );
33
				}
34
				el.prop( 'disabled', false );
35
			});
36
		},
37
	};
38
})( jQuery );
39