0

I have a function with an if statement which contains two separate AJAX call operations. Based on a variable called subjectType, the conditions for the call change. If I'm trying to get user information I need to give it the userLoginName. If I'm trying to get Site information I need to give it the webURL.

I can accomplish this using an if statement but is there a better way to do this without having to repeat most of the lines?

var subjectType = if(/*I'll check the value of the drop down to see if a user or site report was asked for*/)?"GetGroupCollectionFromUser":"GetGroupCollectionFromWeb";

function parseSubjectColFromUser(subject){
  var subjectGroups = [];
  if (subjectType){
    $().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: subject,
      completefunc: function(xData, Status){
        $(xData.responseXML).find("Group").each(function(){
            subjectGroups.push($(this).attr('Name'));
        });
      }
    });
  }else{
    $().SPServices({
      operation: "GetGroupCollectionFromWeb",
      webURL: subject,
      completefunc: function(xData, Status){
        $(xData.responseXML).find("Group").each(function(){
            subjectGroups.push($(this).attr('Name'));
        });
      }
    });
  }
  return subjectGroups;
}
2
  • 1
    Couldn't you just compose $().SPServices({... into a function and put invocations in both conditional branches? Commented Nov 6, 2013 at 18:26
  • Possibly, but I have no idea what that means Commented Nov 6, 2013 at 18:27

1 Answer 1

3

Should be pretty easy...I also assumed you wanted to use a callback function here since you're doing this via an AJAX request.

function parseSubjectColFromUser(subject, myCallback){
    var subjectGroups = [];
    var params = {
        operation: subjectType ? "GetGroupCollectionFromUser" : "GetGroupCollectionFromWeb",
        completefunc: function(xData, Status){
            $(xData.responseXML).find("Group").each(function(){
                subjectGroups.push($(this).attr('Name'));
            });
            myCallback(subjectGroups);
        }
    };

    if (subjectType) {
        params.userLoginName = subject;
    } else {
        params.webURL = subject;
    }

    $().SPServices(params);
}
Sign up to request clarification or add additional context in comments.

7 Comments

Sorry, my question might not have been clear, but setting the operation isn't the problem, the problem is that based on the operation, i'm either using userLoginName or webURL. Setting that is what I'm not sure how to do efficiently.
Something like this..got a little mixed up with brackets & parenthesis
Hey Cameron, thanks for the reply, ill give this a shot. In regards to the callback issue, this function is running inside another function and the way it's called is through a var X = parseSubjectColFromUser(subject[i]), then the return is stored in variable X. Is a callback still necessary?
A callback is only unnecessary if you don't need to do anything right when you get the data back. If you want to react somehow when the data comes back from the AJAX request, use a callback function.
Oh okay I see. Here I just need to store the ajax result in to a variable that's why I think a return is more appropriate. But yea, now I know when to you a callback so that's cool. Thanks man.
|

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.