Skip to content

Commit b9f37e8

Browse files
committed
refactor how data from form is gotten/sent. Should fix #218
1 parent c2aa92b commit b9f37e8

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

assets/js/epoch.js

+44-25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ jQuery( document ).ready( function ( $ ) {
55
epoch.init();
66
}
77

8+
$.fn.EpochserializeObject = function() {
9+
var arrayData, objectData;
10+
arrayData = this.serializeArray();
11+
objectData = {};
12+
13+
$.each(arrayData, function() {
14+
var value;
15+
16+
if (this.value != null) {
17+
value = this.value;
18+
} else {
19+
value = '';
20+
}
21+
22+
if (objectData[this.name] != null) {
23+
if (!objectData[this.name].push) {
24+
objectData[this.name] = [objectData[this.name]];
25+
}
26+
27+
objectData[this.name].push(value);
28+
} else {
29+
objectData[this.name] = value;
30+
}
31+
});
32+
33+
return objectData;
34+
};
35+
836
} );
937

1038

@@ -249,6 +277,9 @@ function Epoch( $, EpochFront ) {
249277
var fail = false;
250278
var fails = [];
251279

280+
281+
282+
252283
$form.find( 'select, textarea, input' ).each( function () {
253284
if ( !$( this ).prop( 'required' ) ) {
254285

@@ -272,37 +303,22 @@ function Epoch( $, EpochFront ) {
272303
} );
273304
}
274305
} else {
275-
var data = {
276-
content: $( '#comment' ).val(),
277-
post: EpochFront.post,
278-
author_name: '',
279-
author_email: '',
280-
author_url: '',
281-
epoch: true,
282-
parent: $( '#comment_parent' ).val(),
283-
_wpnonce: EpochFront._wpnonce
284-
};
285-
286-
var authorEL = document.getElementById( 'author' );
287-
if ( null !== authorEL ) {
288-
data.author_name = $( authorEL ).val();
289-
}
306+
var data = $form.EpochserializeObject();
290307

291-
var emailEl = document.getElementById( 'email' );
292-
if ( null !== emailEl ) {
293-
data.author_email = $( emailEl ).val();
294-
}
295-
296-
var urlEl = document.getElementById( 'url' );
297-
if ( null !== urlEl ) {
298-
data.author_url = $( urlEl ).val();
299-
}
300308

309+
data.author_name = data.author;
310+
data.author_url = data.url;
311+
data.post = data.comment_post_ID;
312+
data.parent = data.comment_parent;
313+
data.content = data.comment;
301314
if ( 0 != EpochFront.user_email ) {
302-
data.author_email = EpochFront.user_email
315+
data.author_email = EpochFront.user_email;
316+
}else{
317+
data.author_email = data.email;
303318
}
304319

305320
data.author_email = encodeURI( data.author_email );
321+
delete data.author;
306322

307323
$.post( EpochFront.comments_core, data ).done( function ( r, textStatus, rObj ) {
308324

@@ -434,3 +450,6 @@ function Epoch( $, EpochFront ) {
434450
};
435451

436452
}
453+
454+
455+

0 commit comments

Comments
 (0)