Skip to content

Instantly share code, notes, and snippets.

@kara-ryli
Created March 16, 2012 18:54
Show Gist options
  • Save kara-ryli/2051832 to your computer and use it in GitHub Desktop.
Save kara-ryli/2051832 to your computer and use it in GitHub Desktop.
Asynchronously load and initialize Facebook sanely using YUI.
YUI.add("facebook", function (Y) {
"use strict";
var isFunction = Y.Lang.isFunction,
queue = [],
win = Y.config.win,
FB;
function queueInArray(callback) {
queue.push(callback);
}
function fbIsLoaded(callback) {
if (isFunction(callback)) {
callback(FB);
}
}
function asyncInit() {
FB = win.FB;
FB.init({
appId: FACEBOOK_APP_ID,
channelUrl: FACEBOOK_CHANNEL_PATH,
status: true,
cookie: true,
xfbml: true
});
Y.loadFacebook = fbIsLoaded;
Y.Array.each(queue, fbIsLoaded);
}
function firstLoad(callback) {
Y.loadFacebook = queueInArray;
queueInArray(callback);
if (!Y.one("#fb-root")) {
Y.one("body").insert("<div id=\"fb-root\" style=\"position:absolute;top:0;left:0;\"></div>", 0);
}
win.fbAsyncInit = asyncInit;
Y.Get.script("//connect.facebook.net/en_US/all.js");
}
Y.loadFacebook = firstLoad;
}, "3.4.1", { requires: ["node-base"] });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment