I have several selections that I populate with jQuery Ajax. The heavy load is thin. However, there are one or two of these queries that, in a few rare cases, return LOT records for selection. I was wondering if the way I populate the selection is the most efficient way to do this from client side code.
I skipped some things to make the code shorter.
$(function () {
FillAwcDll()
});
function FillAwcDll() {
FillSelect('poleDdl', 'WebService.asmx/Pole', params, false, null, false);
}
function ServiceCall(method, parameters, onSucess, onFailure) {
var parms = "{" + (($.isArray(parameters)) ? parameters.join(',') : parameters) + "}";
var timer = setTimeout(tooLong, 100000);
$.ajax({
type: "POST",
url: appRoot + "/services/" + method,
data: parms,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
clearTimeout(timer);
alert("success");
if (typeof onSucess == 'function' || typeof onSucess == 'object')
onSucess(msg.d);
},
error: function (msg, err) {
}
}
});
function FillSelect(sel, service, param, hasValue, prompt, propCase) {
var selectId = 'select#{0}'.format(sel);
if ($(selectId) == null) {
alert('Invalid FillSelect ID');
return;
}
$(selectId + ' option').remove();
$('<option class=\'loading\' value=\'\'>loading...</option>').appendTo(selectId);
ServiceCall(service,
param,
function (data, args) {
$(selectId + ' option').remove();
if (prompt != null && prompt.length > 0) {
$('<option class=\'selectPrompt\' value=\'\' selected>{0}</option>'.format(prompt)).appendTo(selectId);
}
$.each(data, (hasValue)
? function (i, v) {
$('<option value=\'{0}\'>{1}</option>'.format(v.Key, (propCase) ? v.Value.toProperCase() : v.Value)).appendTo(selectId);
}
: function (i, v) {
$('<option value=\'{0}\'>{1}</option>'.format(v, (propCase) ? v.toProperCase() : v)).appendTo(selectId);
})
},
FailedServiceCall);
}
String.prototype.format = function () {
var pattern = /\{\d+\}/g;
var args = arguments;
return this.replace(pattern, function (capture) { return args[capture.match(/\d+/)]; });
}
Thus, it only loops around and fills the fetch. Is there a better way to do this? Pay attention to the warning fire ("success") almost immediately, so the data returns quickly, but then hangs after that, trying to fill out the selected option.
: (3) . . onBlur ( ), onBlur , FOREVER , ... , ?
ServiceCall(service,
param,
function (data, args) {
var $select = $(selectId);
var vSelect = '';
if (prompt != null && prompt.length > 0) {
vSelect += '<option class=\'selectPrompt\' value=\'\' selected>{0}</option>'.format(prompt);
}
if (hasValue) {
$.each(data, function (i, v) {
vSelect += '<option value=\'{0}\'>{1}</option>'.format(v.Key, (propCase) ? v.Value.toProperCase() : v.Value);
});
}
else {
$.each(data, function (i, v) {
vSelect += '<option value=\'{0}\'>{1}</option>'.format(v, (propCase) ? v.toProperCase() : v);
});
}
$select.html(vSelect);
delete vSelect;
delete data;
},
FailedServiceCall);
}