The function getProfile invokes FB API function FB.api which executes an asynchoronous HTTP request. In your loadProfile function call you call getProfile which immediately returns facebook.profile object which is not populated with data yet since the HTTP request is not finished yet.
Consider following change:
getProfile : function(fCallback) {
var bInfo = false,
bPhotos = false;
FB.api('/me', function(response) {
facebook.profile.user_id = response.id;
facebook.profile.name = response.name;
facebook.profile.firstName = response.first_name;
facebook.profile.lastName = response.last_name;
facebook.profile.gender = response.gender;
bInfo = true;
if (bPhotos)
fCallback(facebook.profile);
});
FB.api('/me/photos', {limit: 8}, function(response) {
facebook.profile.numPhotos = response.data.length;
for (key in response.data) {
var photoUrl = response.data[key].source;
eval('facebook.profile.photo' + key + '= photoUrl');
}
bPhotos = true;
if (bInfo)
fCallback(facebook.profile);
});
}
and call this function the following way now:
function loadProfile() {
facebook.getProfile(function (profile) {
alert(profile.name);
});
}
The reason why ou could see fields in the console is because you introspected the object after the asynch call was successfully executed. The alert call however executed immediately in the same thread on a not yet populated object.
Sergey Ilinsky
2010-07-23 20:09:55