我还发现我的 Facebook 对话框在 Ajax 加载后停止触发,但这与 Facebook 无关。使用 Turbolinks,您必须在事件上绑定您的点击处理程序page:load
(或者可能在 body 元素上使用事件委托) - 否则它们会丢失。请参阅Turbolinks 上的 Railscast以及“页面更改事件”。
例子:
项目.js.coffee.erb
ready = ->
$('a').click (event) ->
alert('do something facebooky')
event.preventDefault()
$(document).ready(ready)
$(document).on('page:load', ready)
就我而言,我还必须改变 Facebook 的加载方式。请参阅此问题和相应的解决方案。这是我的画布应用程序中的代码。
应用程序.html.erb
<!-- I used no other facebook code -->
<div id="fb-root"></div>
facebook.js.coffee.erb
#
# Get Facebook working with Turbo Links
# see: http://reed.github.io/turbolinks-compatibility/facebook.html
#
fb_root = null
fb_events_bound = false
$ ->
loadFacebookSDK()
bindFacebookEvents() unless fb_events_bound
bindFacebookEvents = ->
$(document)
.on('page:fetch', saveFacebookRoot)
.on('page:change', restoreFacebookRoot)
.on('page:load', ->
FB?.XFBML.parse()
)
fb_events_bound = true
saveFacebookRoot = ->
fb_root = $('#fb-root').detach()
restoreFacebookRoot = ->
if $('#fb-root').length > 0
$('#fb-root').replaceWith fb_root
else
$('body').append fb_root
loadFacebookSDK = ->
window.fbAsyncInit = initializeFacebookSDK
$.getScript("//connect.facebook.net/en_US/all.js#xfbml=1")
initializeFacebookSDK = ->
FB.init
appId : "<%= ENV['FACEBOOK_APP_ID'] %>"
channelUrl: "//" + window.location.host + "/channel.html"
status : true
cookie : true
xfbml : true
frictionlessRequests : true
# Listen to the auth.login which will be called when the user logs in
# using the Login button
FB.Event.subscribe 'auth.login', (response) ->
# We want to reload the page now so Ruby can read the cookie that the
# Javascript SDK sat. But we don't want to use
# window.location.reload() because if this is in a canvas there was a
# post made to this page and a reload will trigger a message to the
# user asking if they want to send data again.
window.location = window.location
FB.Canvas.setAutoGrow()