codersgrave / Facebook-Friend-Selector

This repo is no longer maintained. Feel free to fork and update!
http://facebook-friend-selector.codersgrave.com
64 stars 22 forks source link

Can't get name on response #4

Closed daltonpereira closed 11 years ago

daltonpereira commented 11 years ago

how to access full name on response ? Tried response.name but it returns undefined.

sevilyilmaz commented 11 years ago

response value only returns selected Ids as an array. If you want full name you can fork the plugin and change the "_submit" method like this:

var selected_friends = [];
$('input.fs-friends:checked').each(function(){
  var self = $(this),
      id = self.val().split('-')[1],
      fullName = self.siblings('.fs-name').text();

  selected_friends.push({
    id: parseInt(id, 10),
    fullName: fullName
  });
});

if ( fsOptions.facebookInvite === true ){

  var friends = $.map(selected_friends, function ( v ) {
    return v.id;
  }).join();

  FB.ui({
    method: 'apprequests',
    message: fsOptions.lang.facebookInviteMessage,
    to: friends
  },function(response){

    if ( response !== null ){
      fsOptions.onSubmit(selected_friends);

      if ( fsOptions.closeOnSubmit === true ) {
        _close();
      }
    }

  });
}
else{
  fsOptions.onSubmit(selected_friends);

  if ( fsOptions.closeOnSubmit === true ) {
    _close();
  }
}

This will give you both id and full name as an array like this:

[
  {
    id: 123,
    fullName: 'Lorem'
  },
  {
    id: 234,
    fullName: 'Ipsum'
  }
]

and if you only want to use full name you can get with a for loop or $.map method of jQuery etc. like this:

var names = $.map(response, function( v ) {
  return v.fullName;
}).join();