0

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());
        }
    }
}
2
  • Really you have 4 parameters as id and locationid are passed to getEntityData with different names. Are they the same thing? You haven't shown what getEntityData does. Commented Apr 24, 2014 at 22:26
  • I will update now. The getEntityData is just an ajax function which takes the data in the data: section. Commented Apr 24, 2014 at 22:31

4 Answers 4

4

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
});
Sign up to request clarification or add additional context in comments.

3 Comments

this looks awesome. Will try it and let you know!!
At this point, having the getLocation function is useless, isn't it? He could just call getEntityData directly.
That is what i was thinking as well @ColinDeClue. Thanks for the update :)
3

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);
}

Comments

3
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');

2 Comments

just be aware that if you are doing this in an existing codebase that actively uses these to function that the function signature (order of arguments is different). you will need to refactor your existing code.
Might be better to make the optional argument last so it doesn't need to be elided.
3

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);
}

1 Comment

shouldn't that be 'clientbrandid': _clientid? Otherwise it's just an unused variable, correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.