Skip to content

Instantly share code, notes, and snippets.

@kirqe
Created July 14, 2015 21:30
Show Gist options
  • Save kirqe/487f413da79d7c8c93f0 to your computer and use it in GitHub Desktop.
Save kirqe/487f413da79d7c8c93f0 to your computer and use it in GitHub Desktop.
Async twitter widget with rails turbolinks
<!-- before: -->
<a class="twitter-timeline"
href="https://twitter.com/twitterapi"
data-widget-id="YOUR-WIDGET-ID">Tweets by @twitterapi</a>
<!-- after: -->
<div class="twiiter-timeline-container"
data-widget-id="YOUR-WIDGET-ID"
data-widget-options="<%= { borderColor: '#abcdef' }.to_json %>">
</div>
//app/assets/javascripts/twitter.js.coffee
$ ->
loadTwitterSDK()
$(document).on 'page:change', renderTimelines
loadTwitterSDK = ->
$.getScript "//platform.twitter.com/widgets.js", ->
renderTimelines()
renderTimelines = ->
$('.twitter-timeline-container').each ->
$container = $(this)
widgetId = $container.data 'widget-id'
widgetOptions = $container.data 'widget-options'
$container.empty()
twttr?.widgets.createTimeline widgetId, $container[0], null, widgetOptions
@borisrorsvort
Copy link

Would you have an updated version of this one?

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