Reputation: 10880
This might be strange, but what I am looking for is how would I make the scope of jQuery.ajaxSetup()
inside a specific object only. Let me give you an example, I have two objects obj_1
and obj_2
. I want to write ajaxSetup in such a way that all my ajax calls inside an object will use one set of params and all my calls in second object will use other set of params.
Eg
var obj_1 = {
my_func : function(){
$.ajax({
success : function(){
// do something
// all ajax calls in this object should use one set of params (type,dataType,headers etc)
}
})
}
};
var obj_2 = {
my_func : function(){
$.ajax({
success : function(){
// do something
// all ajax calls in this object should use different params (type,dataType,headers etc)
}
})
}
};
I want to do this so that I don't have to write all params in every ajax call.
Upvotes: 3
Views: 547
Reputation: 1074989
Your best bet is to create a function for use by the object and have it set the parameters when doing the ajax call. The object uses that function rather than calling ajax
directly. E.g., a simple wrapper function.
Alternately, you could have a standardParams
property on each object that you reuse, perhaps mixing in the params that are specific to the call. Something like:
var obj_x = {
standardParams: {
// These are the params that don't vary
dataType: "json" // Or whatever
},
your_func: function() {
$.ajax($.extend({}, this.standardParams, {
// These are the params that vary
success: function() { ... }
}));
}
};
(Note the use of jQuery.extend
to mix the per-call and standard params.)
Or of course, you could use a combination of the two (a wrapper function, which uses a standardParams
property under the covers).
Upvotes: 3