Hi before opening this issue , I went through many posts from stackoverflow. But i couldn’t find my desired answer.
So Let me introduce my problem first.
I have a piece of code written in JSON object class.
In which you can see there is customers object having its function for adding New Customer. And a method for request Ajax calls
var sys={
customers:{
addNew:function (ref,cb=null){
if(!cb) { // so it can check if the call to this method was for requesting ajax request or handling its response . note i am sending the callback function reference same as the current
core.request({d: $('form').serialize()}, 'sys.customers.addNew', ref);
}else{
if(ref.status){
$('.customers-list').append('<li>'+ref.customer.name+'</li>');
alert('success')
}
}
},
updateRowAfterAdd:function (){
// or i could use this for handling callback by passing its reference instead of the upper same function
}
},
request: function (p = {}, c = null,e=false) {
$.ajax({
url: "/to/my/server",
data: {p: p},
type: 'post',
dataType: 'json',
beforeSend: function () {
},
success: function (r) {
if (c != null)
(e?eval("(" + c + "(r,e));"):eval("(" + c + "(r));"));
}
});
}
}
Here is more explanation. If i want to use it i would go like this
$(document).on('click','.addNew',function () {
sys.customers.addNew($(this));
});
So the idea in this example is to call the ajax method by passing a callback function reference for handling the SUCCESS response after it gets completed.
And if you look at addNew() method. It is working two way.. With the help of 2nd param ( cb ) it is determining that the call to this function was for sending Ajax request or handling its response back .
As i’m using in ajax’s success method eval() which is i know evil .
So i want to understand how i can do this without using EVAL method ? Because i have multiple things running on my page which needs ajax calls. and i don’t want them to rewrite for each of them.
And also i need this for ajax’s beforeSuccess() method as well.