I'm trying to figure out how to pass my callback additional parameters. So far I have the following code.
$("#refine_mp_type").change(function(){
var mp_type = $(this).val();
var country_id = $('#refine_country').val();
var dropdown = $('#refine_grade').find('select');
getMPTypeGrades(country_id, mp_type, populateGradesDropdown);
});
function getMPTypeGrades(country_id, mp_type, callback){
display_wait_dialog ("Please Wait", "Loading grades...");
$.ajax(
{
//do ajax
})
.done(function(data){
callback(data);
})
.fail(//do something)
.always(//do something);
}
populateGradesDropdown(data){
//populate the dropdown with the returned data
}
My thoughts so far on how to achieve this are:
- I could set the dropdown variable to global scope and access it that way(Will pollute global scope).
- I could pass the dropdown element to getMPTypeGrades function and then pass it to the callback function. (I want to keep getMPTypeGrades as generic as possible. I don't know if I'll always be passing a callback that accepts a JQuery element.)
What is the best method for passing the JQuery element and the Ajax response as parameters for my callback?