Created
September 20, 2012 11:16
-
-
Save daveespionage/3755281 to your computer and use it in GitHub Desktop.
Facebook Post To Feed functionality for Javascript with data attribute overrides
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global $:false */ | |
/*global FB:false */ | |
function postToFeed(sourceObj) { | |
// using code from https://developers.facebook.com/docs/reference/dialogs/feed/ | |
// calling the API ... | |
var obj = { | |
method: 'feed', | |
link: '{url}', | |
picture: '{photo.png}', | |
name: '{site_name}', | |
caption: '{site_caption}', | |
description: '{site_description}' | |
}; | |
// check for optional overrides to obj | |
var props = ['description','link','picture']; | |
var getProp = function(propname,src){ | |
return $(src).attr('data-facebook-' + propname); | |
}; | |
var setProp = function(propname,propvalue){ | |
if(propvalue !== undefined){ | |
if(propvalue !== ''){ | |
obj[propname] = propvalue; | |
return true; | |
} | |
} | |
return false; | |
}; | |
for (var i = 0; i < props.length; i+= 1) { | |
setProp(props[i],getProp(props[i], sourceObj)); | |
} | |
function callback(response) { | |
// handle response | |
// document.getElementById('msg').innerHTML = "Post ID: " + response['post_id']; | |
} | |
FB.ui(obj, callback); | |
return false; | |
} | |
// Initialization of ui elements | |
// Facebook post functionality | |
FB.init({appId: "{app_id}", status: true, cookie: true}); | |
$('.facebook-icon').on('click.fbshare',function(){ | |
postToFeed(this); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires FB api