I am trying to provide a call back function for an ajax call, where the function name is saved under the "data-apply" attribute of a form.
jQuery(function($) {
$('form[data-async]').on('submit', function(event) {
var $form = $(this);
var $target = $($form.attr('data-target'));
var apply = $form.attr('data-apply');
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
success: function(data, status) {
var callBackFunc = new Function(apply);
callBackFunc.call();
}
});
event.preventDefault();
});
});
function addBubble(){
alert('a');
}
The form:
<form id="111" class="form-horizontal " data-async data-target="#rating-modal" data-apply="addBubble();" action="/some/action/performed" method="POST">
I want to avoid using eval() in this case, since eval() might introduce security and performance issues. Not sure Function() is a safer method or there is a completely different way to handle this.
So is there anyway to pass in a function callback in order to handle difference situations?
The original code was borrowed from: https://gist.github.com/havvg/3226804