Skip to content

Instantly share code, notes, and snippets.

@Whoaa512
Last active August 7, 2017 17:44
Show Gist options
  • Save Whoaa512/4597095 to your computer and use it in GitHub Desktop.
Save Whoaa512/4597095 to your computer and use it in GitHub Desktop.
Meteor code to user FB.ui & the FB SDK
<body>
{{#constant}}
<div id="fb-root"></div>
{{/constant}}
<div> {{> fb_pic}} {{loginButtons}}</div>
<br />
<div><h2>Click to post to your feed:</h2> {{> feed}} </div>
</body>
<template name='feed'>
<input class='post' type='submit' value='Feed Post!' />
</template>
var fbSdkLoader = function() {
if(!Session.get("is Facebook JDK loaded?")) { // Load Facebook JDK only once.
Session.set("is Facebook JDK loaded?", true);
window.fbAsyncInit = function() { // See Facebook JavaScript JDK docs at: https://developers.facebook.com/docs/reference/javascript/
// Init the FB JS SDK
var initConfig = {
appId: 'YOUR_APP_ID', // App ID from the App Dashboard
channelUrl: Meteor.absoluteUrl("channel.html"), // channel url for FB
status: false, // check the login status upon init?
cookie: false, // set sessions cookies to allow your server to access the session?
xfbml: false // parse XFBML tags on this page?
};
FB.init(initConfig);
};
(function(d, debug) { // Load the SDK's source Asynchronously
var js, id = 'facebook-jssdk',
ref = d.getElementsByTagName('script')[0];
if(d.getElementById(id)) {
return;
}
js = d.createElement('script');
js.id = id;
js.async = true;
js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));
}
};
fbSdkLoader(); // run the loader
Template.feed.events({ // wiring for the button
'click .post': function(e) {
e.preventDefault();
FB.ui({
method: 'feed',
name: 'Facebook Dialogs',
link: 'http://fbrell.com/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
}, function(response) {
if(response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
});
}
});
Meteor.startup(function() { // serve channel.html file programmatically
var connect;
connect = Npm.require("connect");
__meteor_bootstrap__.app.use(connect.query()).use(function(req, res, next) {
// Need to create a Fiber since we're using synchronous http
// calls and nothing else is wrapping this in a fiber
// automatically
return Fiber(function() {
if(req.url === "/channel.html") {
res.writeHead(200, {
'Content-Type': 'text/html'
});
return res.end('');
} else {
return next(); // if not a channel.html request, pass to next middleware.
}
}).run();
});
});
@isbkch
Copy link

isbkch commented Feb 9, 2014

Great ! thank you !

@soncco
Copy link

soncco commented Sep 28, 2014

Deprecated, because {{#constant}} has been removed

@hems
Copy link

hems commented Jan 5, 2015

will this work with cordoba applications? if not, is there a quick way to implement it ( as in: no additional packages )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment