I'm trying to send different values in my data
parameter for autocomplete, depending on a previously set global variable lookupType
.
However, all of the other code in repeated even though only the data
section is different.
How do I reduce the redundant code?
In one case the data
section is this:
data: {
type: "full",
location: "local",
name: request.term
},
while in开发者_高级运维 another it is like this:
data: {
append: "no",
doPreprocess: true,
name: request.term,
maxResults: 1000
},
The full code is below:
$( "#lookup" ).autocomplete({
if($("#hiddenLookupType").val() == "order")
{
source: function( request, response ) {
$.ajax({
url: lookupUrl,
dataType: "jsonp",
data: {
type: "full",
location: "local",
name: request.term
},
success: function( data ) {
// do something
}));
}
});
},
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
}
else if($("#hiddenLookupType").val() == "inventory")
{
source: function( request, response ) {
$.ajax({
url: lookupUrl,
dataType: "jsonp",
data: {
append: "no",
doPreprocess: true,
name: request.term,
maxResults: 1000
},
success: function( data ) {
// do something
}));
}
});
},
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
}
});
instead of:
data: {
type: "full",
location: "local",
name: request.term
},
have:
data: GetData(this),
function GetData(el)
{
..Logic Here
}
//calling it:
var parameters = {
type: "full",
location: "local",
name: request.term
}
test(parameters);
//the method
function test(dataList){
$( "#lookup" ).autocomplete({
if($("#hiddenLookupType").val() == "order")
{
source: function( request, response ) {
$.ajax({
url: lookupUrl,
dataType: "jsonp",
data: dataList,
success: function( data ) {
// do something
}));
}
});
},
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
}
else if($("#hiddenLookupType").val() == "inventory")
{
source: function( request, response ) {
$.ajax({
url: lookupUrl,
dataType: "jsonp",
data: {
append: "no",
doPreprocess: true,
name: request.term,
maxResults: 1000
},
success: function( data ) {
// do something
}));
}
});
},
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
}
});
}
精彩评论