My knowledge of AJAX calls is limited, so I am unsure about how they interact with array filtering using A.filter()
. The array in question is used to populate a template synchronously.
// An event triggers a function to filter a list on the page.
// The function then calls filterArray(arrayList, objFilters)
async_fetch: function(string)
{
// Uses $.ajax() to retrieve a JSON array
var deferred = $.Deferred();
$.ajax({
url: ...,
dataType: "json",
success: function(data) {
var response = data;
deferred.resolve(data);
},
error: function(data)
{
//...
deferred.reject(msg);
}
});
return deferred;
};
filterArray: function(list, filters)
{
var filteredList = list.filter(function(item) {
for(var key in filters) {
// Actions for multiple filters to compare against...
else if(key == 'FILTER_X') {
var data = async_fetch(item.name);
// Using data to make a determination where true means excluding the item from the filtered list
if(determination)
return false;
}
}
};
return filteredList;
};
// Results of filterArray() are sent to a template within Backbone
// to update an HTML segment on the page.
Will the call to filter wait synchronously for the AJAX call to complete? Will the list be filtered and returned without waiting for the AJAX call to finish, requiring the AJAX call to integrate into the filtered list later?
Should I create a non-async version of async_fetch()
instead?