Skip to content

Instantly share code, notes, and snippets.

@bloudermilk
Forked from patbenatar/fb_init.coffee
Created August 24, 2012 21:45
Show Gist options
  • Save bloudermilk/3455989 to your computer and use it in GitHub Desktop.
Save bloudermilk/3455989 to your computer and use it in GitHub Desktop.
Initialize Facebook JS SDK in Coffeescript
@FBootloader = new class FBootloader
callbacks: []
isReady: false
ready: ->
callback() for callback in @callbacks
bind: (callback) ->
if @isReady
callback()
else
@callbacks.push callback
@fbAsyncInit = ->
FB.init
appId: document.getElementById("fb-root").getAttribute("data-id")
FBootloader.ready()
# Async load the JS SDK
id = "facebook-jssdk"
ref = document.getElementsByTagName("script")[0]
unless document.getElementById(id)
js = document.createElement("script")
js.id = id
js.async = true
js.src = "//connect.facebook.net/en_US/all.js"
ref.parentNode.insertBefore(js, ref)
<!DOCTYPE html>
<html>
<head>
<title>My awesome webpage!</title>
<script src="/fb_init.js"></script>
<script src="/something_rad.js"></script>
</head>
<body>
<div id="fb-root" data-id="1234567890"></div>
</body>
</html>
FBootloader.bind -> FB.someRadThing("Foo bar baz!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment