How to get action id to delete previously posted action on timeline - javascript

I am using following code to post actions on timeline
<script type="text/javascript">
function read()
{
FB.api('/me/app_namespace:read' +
'?article=http://example.com/test.php&access_token=','post',
function(response) {
var msg = 'Error occured';
if (!response || response.error) {
if (response.error) {
msg += "\n\nType: "+response.error.type+"\n\nMessage: "+response.error.message;
}
alert(msg);
}
else {
alert('Post was successful! Action ID: ' + response.id);
}
});
}
</script>
Now i want to use the following code to delete action but the problem is dont know how to save the "action id" from above code so that it can be used in following code.
<script type="text/javascript">
function deleteAction()
{
FB.api(
'/actionid',
'delete',
function(response) {
alert('action deleted')
});
}
</script>
Thanks!

It depends, are you looking to delete the action in a later session or current? In the current session itself, you can probably simply store it in a variable. For eg:
function read()
{
FB.api('/me/app_namespace:read' +
'?article=http://example.com/test.php&access_token=','post',
function(response) {
var msg = 'Error occured';
if (!response || response.error) {
if (response.error) {
msg += "\n\nType: "+response.error.type+"\n\nMessage: "+response.error.message;
}
alert(msg);
}
else {
alert('Post was successful! Action ID: ' + response.id);
var idToDeleteLater = response.id;
}
});
}
function deleteAction()
{
FB.api(
'/'+idToDeleteLater,
'delete',
function(response) {
alert('action deleted')
});
}
As you can see, that's pretty straightforward, all you've done is stored the actionID in a variable. You can potentially store multiple IDs in an array or something.
On the other hand, if you want to delete the actions in a later session (unlikely for a "read" action), then you need to store these action IDs in a database or something, and pull the action from there prior to deleting.

Related

How can I replace prompt with dialog and promises JS

I have next method that is triggered on button click
function approveDay(URL, dateDay, action, cNT) {
var message = '';
if (action === 'false') {
message = prompt('Enter notes : ', '');
if (message === null) {
return false;
} else if (message.trim() === "") {
showMessage(2,
"The status of the timesheet cannot be changed to \"Not approved\" if no comments are specified!");
return false;
}
}
var data;
if (cNT === null) {
data = addAntiForgeryToken({ UID: userId, dateDay: dateDay, action: action, message: message });
}
else {
data = addAntiForgeryToken({ UID: userId, dateDay: dateDay, action: action, message: message, cNT: cNT });
}
blockUI();
$.ajax({
type: 'POST',
url: URL,
data: data,
success:
function (result) {
showMsg(result);
location.reload();
},
error: function (xhr, textStatus, errorThrown) { AjaxErrMessage(xhr, textStatus, errorThrown); }
});
return false;
}
I need to remove prompt and replace it with dialog box, which contains input field for message and button for add that saves this message. But also I need to stop the execution of the function. I know than promises can help me with my problem.
How can I implement this solution or solution without promises. My dialog box initialize function is below
function initModalWindow() {
$("#RejectNotesBlock").dialog({
width: 500,
modal: true,
close: function (event, ui) {
$("#RejectNotesBlock").dialog("close");
}
});
}
Thank you.

Facebook JS SDK check specific like of logged in user

I can't seem to check if a logged in user likes a specific Facebook Business page(URL). This keeps coming back as undefined. Notice anything wrong here?
function postLike() {
FB.api(
'https://graph.facebook.com/me/og.likes',
'get',
{ id: objectToLike,
privacy: {'value': 'SELF'} },
function(response) {
if (!response) {
alert('Error occurred.');
} else if (response.error) {
document.getElementById('result').innerHTML =
'Error: ' + response.error.message;
} else {
document.getElementById('result').innerHTML =
'Business page is ' +
response.object + '</a>';
}
}
);
}

Calling Ajax method using javascript function

I'm working with jQuery Mobile.
My Code for move to details div ::
Go To Details
Now, I want to authenticate user with Facebook.
So I have applied ::
Login with Faceook
Function Calling for Authentication
function userLogin() {
FB.login(
function(response) {
if (response.authResponse) {
alert('logged in');
FB.api('/me', function(response) {
alert("Welcome " + response.name);
});
} else {
alert('not logged in');
}
},
{ scope: "email" }
);
}
I'm getting valid user response after authentication.Now I want to navigate to that details div.
So, how can I implement that ajax code after successfully login ??
Any suggestion will be appreciated.
Thanks.
Instead of changing the href attribute directly, you should bind the onclick event to a function returning true.
Modify your a tag.
Go To Details
Modify your userLogin function:
function userLogin() {
FB.login(
function(response) {
if (response.authResponse) {
alert('logged in');
FB.api('/me', function(response) {
alert("Welcome " + response.name);
});
} else {
alert('not logged in');
}
},
{ scope: "email" }
);
return true;
}
It seems that FB.login is an asynchronized operation. If you want to redirect after the login action, try to modify the location.hash after login. Note that the return value has changed to false.
function userLogin() {
var hash = '#details';
FB.login(
function(response) {
if (response.authResponse) {
alert('logged in');
FB.api('/me', function(response) {
alert("Welcome " + response.name);
window.location.hash = hash;
});
} else {
alert('not logged in');
}
},
{ scope: "email" }
);
return false;
}
Use $.ajax() for making a call to the Facebook API to authenticate and in the callback function of the ajax function, call the click event of the details link.
Maybe something like this,
$.ajax({
url : 'https://graph.facebook.com/me'+whatever,
success : function(){
$('#details').click()
}
});
The success function will be called when the ajax call is successful.

adding Facebook friends to a friendlist with FB.api

How can I add friend(s) to a friendlist using JavaScript SDK?
I found this example for using POST method in order to post the message to the feed:
var body = 'Reading JS SDK documentation';
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
However, I do not understand, how it works. Ehat is 'message' in { message: body } and how it can be adapted to this matrix "/FRIENDLIST_ID/members?members=1,2,3" (this is from FB.api examples ar https://developers.facebook.com/docs/reference/javascript/FB.api/
This is what worked for me. Make sure the key is enclosed within quotes. 'members' in this case. Though I was able to create lists, trying to add members to a created list kept throwing an exception with a message saying 'Manage_friendlists permission required' thought I had that permission. Turns out one needs the publish stream permission as well in order for this to work. This wasn't mentioned anywhere. Hope this helps.
FB.api('/' + friendlistID + '/members', 'post', {'members': members}, function(response) {
console.log(response);
if (!response || response.error) {
alert('Could not save members!');
} else {
alert('Members added to list!');
}
});
Simply change the endpoint, and the data you're sending.
The Graph Endpoint you're using is FriendList
friendlist_id = 1435134523451;
FB.api('/'friendlist_id+'/members', 'post', {members:'comma,separated,ids,of,friends,to,add,to,list'}, function(response) {
console.log(response);
if (!response || response.error) {
alert('Error occured');
} else {
alert('yay!');
}
});
I think
FB.api('/FRIENDLIST_ID/members?members=1,2,3', 'post', function(response) { … });
should do it.

Javascript variable in string output?

The variable x is getting the current url. I need to then put this url into the spot where the xxxx is. How would I go about doing this?
<script type='text/javascript'>
function postListen()
{
var x = ( document.URL );
FB.api(
'/me/beatmushroom:listen',
'post',
{ song: xxxx},
function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Listen was successful! Action ID: ' + response.id);
}
});
}
Simply use the variable in the object literal. While the key is always a string (even without quotes), the value can be any JavaScript epxression:
{ recipe: x },
Just replace 'xxxx' with the variable x
function postCook() {
var x = ( document.URL );
FB.api(
'/me/[YOUR_APP_NAMESPACE]:cook',
'post',
{ recipe: x },
function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Cook was successful! Action ID: ' + response.id);
}
}
);
}

Categories

Resources