I'm trying to call 2 REST APIs and then populate the select2 dropdown. My select2 code works fine for 1 API call but doesn't if I try to call the 2 API in AJAX results.
The way I'm trying to call the second API looks a but dicey to me but I'm not too sure how do I achieve this. I can nest AJAX calls if not using them in select2 but this is a bit peculiar case.
Any idea on how to achieve that/ or what am I doing wrong here
below is the code for that:
Select2({
disabled : true,
minimumInputLength : 1,
multiple : true,
ajax : {
url : "/rest/api/2/user/picker" ,
type : "GET",
dataType : 'json',
cache : true,
// query parameters for the remote ajax call
data : function data(term) {
return {
query : term,
maxResults : 5,
showAvatar : true
};
},
// parse data from the server into form select2 expects
results : function results(data) {
var i, dataLength, dataUsers;
data = JSON.parse(data);
var users = [];
dataLength = data.users.length;
dataUsers = data.users;
for (var i = 0; i < dataLength; i++)
{
if (dataUsers[i].key != $scope.userInputs.owner.key) {
dataUsers[i].context = CommonConstants.RECIPIENT_AUTH_CONTEXT.USER;
users.push(dataUsers[i]);
}
}
if($scope.someBoolean){
ajax : {
url : "/rest/api/2/groups/picker",
type : "GET",
dataType : 'json',
cache : true,
data : function data(term) {
return {
query : term,
maxResults : 5
};
},
success: function(responseForGroups){
var groupData = JSON.parse(responseForGroups);
var dataGroups, groupsLength;
dataGroups = groupData.groups;
groupsLength = groupData.total;
for (var i = 0; i < groupLength; i++)
{
dataGroups[i].context = CommonConstants.RECIPIENT_AUTH_CONTEXT.GROUP;
dataGroups[i].key = dataGroups[i].name;
dataGroups[i].displayName = dataGroups[i].name;
users.push(dataGroups[i]);
}
}
};
}
return {
results : users
};