i write some function, and i called abApi.general.getUserById
function, but when i print output to $("#friend-requests-block")
my name
variable is empty. i know this is javascript and i try with $("#friend-requests-block")
in getUserById
callback, but then i haven't type
. How can i fix this?
for (var index in requests) {
var name = "";
var type = "";
if(requests[index].FriendRequestTypeId == 1) {
type = "private";
}
else {
type = "business";
}
abApi.general.getUserById(abEnvironment.sessionToken, requests[index].FromUserId, function(response2){
name = response2.Name;
});
$("#friend-requests-blocks").html($("#friend-requests-blocks").html() + "<div class=\"log-block\" id=\"friend-requests-log-"+requests[index].Id+"\"><a href=\"#\"><h2>"+ name + "("+ type +")</h2></a> <div class=\"friend-requests-buttons\" style=\"margin-top: 15px;\"> <a href=\"javascript:void(0)\" data-role=\"button\" style=\"margin: 0px 5px 0px 5px;\" onClick=\"abAction.approveFriendRequest("+requests[index].Id+", 1,"+requests[index].FromUserId+");\">Accept</a><a href=\"javascript:void(0)\"data-role=\"button\" style=\"margin: 0px 5px 0px 5px;\" onClick=\"abAction.approveFriendRequest("+requests[index].Id+", 0,"+requests[index].FromUserId+");\">Reject</a> <a class=\"button3\" href=\"javascript:void(0)\"data-role=\"button\" style=\"margin: 0px 5px 0px 5px;\" onClick=\"abAction.approveFriendRequest("+requests[index].Id+", 2,"+requests[index].FromUserId+");\">Later</a></div></div>");
}
getUserById
probably uses AJAX.name
is set, but after your other code has finished running. Move your$('#friend-request-blocks')
chunk into the same callback function asname
is in and it should work.type
var will be reassigned, too, and he uses it to set the html in$('#friend-request-blocks')
. That's another issue that the OP needs to take into accountresponse2
come from? what doesgetUserById
do? I'd say it either executes a query or requests data from the server-side in some way. In either case: the function that takesresponse2
as an argument is async