tags:

views:

44

answers:

3

I want to put my two functions into one. They look like this now:

function MeYouFriend(){
    $.ajax({ 
       type: "POST",
       url: "misc/AddFriend.php",
    data: {
    mode: 'ajax',
    friend: 'ignore',
    uID : $('#uID').val(),
    fID : $('#fID').val()
    },
       success: function(msg){
$('#friendsPop').jGrowl('close');
        }
     });
}
function MeYouFriendY(){
    $.ajax({ 
       type: "POST",
       url: "misc/AddFriend.php",
    data: {
    mode: 'ajax',
        friend: 'confirm',
    uID : $('#uID').val(),
    fID : $('#fID').val()
    },
       success: function(msg){
$('#friendsPop').jGrowl('close');
        }
     });
}

The difference is only friend: 'confirm' and friend: 'ignore' , how can i put them together to one and make it simple?

+1  A: 

you can pass the string value as an argument:

function MeYouFriend(friend){
    $.ajax({ 
       type: "POST",
       url: "misc/AddFriend.php",
    data: {
    mode: 'ajax',
        friend: friend,
    uID : $('#uID').val(),
    fID : $('#fID').val()
    },
       success: function(msg){
$('#friendsPop').jGrowl('close');
        }
     });
}

then you call MeYouFriend('ignore') or MeYouFriend('confirm')

second
The only issue with this is that it leaves room for error.... ie: MeYouFriend('pancakes'); I think a boolean is most appropriate.
Dutchie432
Hello. I have my link inside a $.jgrowl call, so i cant use '' so when i do <a href="javascript:void(0);" onclick="MeYouFriend('confirm');" ... i get error because of the ' ' in 'confirm'
Karem
There's ways around that too, but I don't think this is the ideal solution for you. Happy Coding.
Dutchie432
+2  A: 

Change your call to

MeYouFriend(true) //To Confirm
MeYouFriend(false) //To Ignore

Than change the function to accept the parameter.

function MeYouFriend(confirm){
    var c = confirm ? 'confirm' : 'ignore';

    $.ajax({ 
       type: "POST",
       url: "misc/AddFriend.php",
       data: {
          mode: 'ajax',
          friend: c,
          uID : $('#uID').val(),
          fID : $('#fID').val()
       },
       success: function(msg){
           $('#friendsPop').jGrowl('close');
        }
     });
}

Alternate Slightly More Compact Version

function MeYouFriend(confirm){
    $.ajax({ 
       type: "POST",
       url: "misc/AddFriend.php",
       data: {
          mode: 'ajax',
          friend: confirm ? 'confirm' : 'ignore',
          uID : $('#uID').val(),
          fID : $('#fID').val()
       },
       success: function(msg){
           $('#friendsPop').jGrowl('close');
        }
     });
}
Dutchie432
works great accepted
Karem
A: 

you can add an parameter, and decide based on the content of it, what to do:

function MeYouFriend(doConfirm){
    $.ajax({ 
       type: "POST",
       url: "misc/AddFriend.php",
    data: {
    mode: 'ajax',
    friend: doConfirm ? 'confirm' : 'ignore',
    uID : $('#uID').val(),
    fID : $('#fID').val()
    },
       success: function(msg){
    $('#friendsPop').jGrowl('close');
        }
     });
}

However you might want to create two access methods which make your code easier to read, and you or others do not have to care about the parameters content:

function MeYouFriendConfirm() {
    MeYouFriend(true);
}
function MeYouFriendIgnore() {
    MeYouFriend(false);
}
Vash
This is a sloppy approach. doConfirm should definitely be a boolean in this case. friend: doConfirm ? 'confirm' : 'ignore',
Dutchie432
You are right! Where is my mind? I even made a big mistake in not using the comparison operator - please forgive, I just edited this mess!
Vash