Is there any simple way to treat the arguments based on entity?
On my function all depends if the second param or third is a function, an object and so. This is my function:
function fbAPI(endpoint, param1, param2, param3, param4) {
var type, params, success, error;
if(typeof param1 !== 'function' && typeof param2 !== 'function')
{
type = param1;
params = param2;
success = param3;
error = param4;
}
if(typeof param1 === 'function')
{
type = 'GET';
params = {};
success = param1;
error = param2;
}
if(typeof param1 === 'object')
{
type = 'GET';
params = param1;
success = param2;
error = param3;
}
if(typeof param1 !== 'function' && typeof param2 === 'function')
{
type = param1;
params = {};
success = param2;
error = param3;
}
FB.api(
endpoint,
type,
params,
function (response) {
if (response && !response.error) {
/* handle the result */
if(typeof success === 'function')
success(response);
}
if (response && response.error) {
console.log(response.error);
/* handle the result */
if(typeof error === 'function')
error(response);
}
}
);
}
Is there any way to make it shorter?
I should can call my function this way:
this.api(_self.endPoints.friends, function (response) {
//do something
});
this.api(_self.endPoints.friends, function (response) {
//do something
},function (response) {
//do something
});
this.api(_self.endPoints.friends, 'GET', function (response) {
//do something
},function (response) {
//do something
});
this.api(_self.endPoints.friends, {data: data}, function (response) {
//do something
},function (response) {
//do something
});
this.api(_self.endPoints.friends, 'GET', {data: data}, function (response) {
//do something
},function (response) {
//do something
});