Skip to content

Commit

Permalink
add JS function sendFormSubmissionAjax to reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
nstjean committed Nov 21, 2019
1 parent 66ba222 commit 070df16
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
9 changes: 9 additions & 0 deletions app/assets/javascripts/ajax.js
@@ -0,0 +1,9 @@
function sendFormSubmissionAjax(dataObj, selector) {
$.ajax({
url: $(selector).attr('action'), // grab the URL from the form itself
data: dataObj,
success: (event, success) => {
$(selector).trigger('ajax:success', event);
}
});
}
3 changes: 2 additions & 1 deletion app/assets/javascripts/application.js
Expand Up @@ -57,4 +57,5 @@
//= require keybindings.js
//= require realtime_username_validation.js
//= require jquery-validation/dist/jquery.validate.js
//= require validation.js
//= require validation.js
//= require ajax.js
9 changes: 2 additions & 7 deletions app/assets/javascripts/comment.js
Expand Up @@ -55,11 +55,6 @@ function insertTitleSuggestionTemplate() {
// JS API for submitting comments
function addComment(comment, selector) {
selector = selector || '.comment-form'; // class instead of id because of the bound function on line 3
$.ajax({
url: $(selector).attr('action'), // grab the URL from the form itself
data: { body: comment },
success: (event, success) => {
$(selector).trigger('ajax:success', event);
}
});
let data = { body: comment };
sendFormSubmissionAjax(data, selector);
}
9 changes: 2 additions & 7 deletions app/assets/javascripts/tagging.js
Expand Up @@ -9,13 +9,8 @@ function addTag(tagname, selector) {
geo = geocodeStringAndPan(place);
}
else {
$.ajax({
url: $(selector).attr('action'), // grab the URL from the form itself
data: { name: tagname },
success: (event, success) => {
$(selector).trigger('ajax:success', event);
}
});
let data = { name: tagname };
sendFormSubmissionAjax(data, selector);
}
}

Expand Down
1 change: 1 addition & 0 deletions config/initializers/assets.rb
Expand Up @@ -2,6 +2,7 @@
'leaflet-blurred-location/dist/Leaflet.BlurredLocation.js',

'advanced_search.js',
'ajax.js',
'application.js',
'comment_expand.js',
'comment.js',
Expand Down

0 comments on commit 070df16

Please sign in to comment.