Created
March 19, 2014 08:38
-
-
Save w0rm/9637709 to your computer and use it in GitHub Desktop.
How to use twitter widgets.js with AMD
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
// Usage: require(['twitter'], function (twitter) { twitter.load() }) | |
define(function() { | |
var twitter = { | |
load: executeWhenReady('load') | |
} | |
// Return wrapped function that will | |
// be executed only when twttr is ready | |
function executeWhenReady (funcName) { | |
return function () { | |
// save args | |
var args = Array.prototype.slice.call(arguments) | |
window.twttr.ready(function () { | |
// call twttr.widgets with args | |
window.twttr.widgets[funcName].apply(window.twttr.widgets, args) | |
}) | |
} | |
} | |
// This is bit of code is from https://dev.twitter.com/docs/tfw-javascript | |
window.twttr = (function (d,s,id) { | |
var t, js, fjs = d.getElementsByTagName(s)[0]; | |
if (d.getElementById(id)) return; js=d.createElement(s); js.id=id; | |
js.src="https://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs); | |
return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } }); | |
}(document, "script", "twitter-wjs")); | |
return twitter; | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment