I have two functions that are very similar and I would like if possible to combine them. The only issue that I have is one function is accepting 2 arguments and the other one is accepting 3. Is there a way to do this or these two functions have to stay separated by design?
function getClientData(id, command) {
var formData = {
'method': command,
'id': id
};
getEntityData(formData);
}
function getLocation(id, clientid, command) {
var formData = {
'method': command,
'locationid': id,
'clientbrandid': clientid
};
getEntityData(formData);
}
Update
function getEntityData(data) {
var url = '/../admin/miscellaneous/components/global.cfc?wsdl';
var ajaxResponse = $.ajax({
url: url,
dataType: 'json',
data: data,
global: false,
async:false,
cache: false,
success: function(apiResponse){
return apiResponse;
}
}).responseJSON;
var response = ajaxResponse[0];
for (var i in response) {
if (response.hasOwnProperty(i)){
$("#edit"+i).val(response[i].trim());
}
}
}
yes you can, I prefer instead of passing each parameter you can pass a js object, and decide wich params it contains for example:
function getLocation(options) {
getEntityData(options);
}
and your call should be:
getLocation({'method': command,'id': id})
Update
or you can just avoid getLocation function and just call getEntityData
getEntityData({
'method': command,
'id': id
});
function getLocation(id, clientid, command) {
var formData = {
'method': command,
'locationid': id
};
if (clientid) {
formData['clientbrandid'] = clientid;
}
getEntityData(formData);
}
// With
getLocation(1, 2, 'doStuff');
// Without
getLocation(1, '', 'doStuff');
Maybe a more rational order of arguments:
function getLocation(id, command, clientid) {
var formData = {
'method': command,
'locationid': id
};
if (clientid) {
formData['clientbrandid'] = clientid;
}
getEntityData(formData);
}
// With
getLocation(1, 'doStuff', 2);
// Without
getLocation(1, 'doStuff');
And if locationid
and id
are different:
function getLocation(id, command, clientid) {
if (clientid) {
var formData = {
'method': command,
'locationid': id,
'clientbrandid': clientid
};
} else {
var formData = {
'method': command,
'id': id,
};
}
getEntityData(formData);
}
// With
getLocation(1, 'doStuff', 2);
// Without
getLocation(1, 'doStuff');
I guess it really depends on what your arguments actually are, but this is also a solution. (Assuming that client id is an object).
function getLocation(id, command, clientid) {
var _clientId = clientid || {};
var formData = {
'method': command,
'locationid': id,
'clientbrandid': _clientid
};
getEntityData(formData);
}
I would go with:
function getWhatever(id, command, clientId) {
var formData = { method: command };
if (typeof clientId === 'undefined') {
formData.id = id;
} else {
formData.locationid = id;
formData.clientbrandid = clientId;
}
getEntityData(formData);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.