Amr*_*ngh 7 javascript jquery ruby-on-rails coffeescript
我在我的rails项目中使用咖啡脚本,但问题是它只在我加载(刷新)页面而不是页面呈现时才有效,它也应该适用于页面视图更改.
这是我正在使用的脚本:
facebook.js.coffee
jQuery ->
$('body').prepend('<div id="fb-root"></div>')
$.ajax
url: "#{window.location.protocol}//connect.facebook.net/en_US/all.js"
dataType: 'script'
cache: true
window.fbAsyncInit = ->
FB.init(appId: env["app_id"], cookie: true)
$('#sign_in').click (e) ->
e.preventDefault()
FB.login (response) ->
window.location = '/auth/facebook/callback' if response.authResponse
$('#sign_out').click (e) ->
FB.getLoginStatus (response) ->
FB.logout() if response.authResponse
true
Run Code Online (Sandbox Code Playgroud)
你可以试试"turbolinks:load":
ready = ->
$('body').prepend('<div id="fb-root"></div>')
$.ajax
url: "#{window.location.protocol}//connect.facebook.net/en_US/all.js"
dataType: 'script'
cache: true
window.fbAsyncInit = ->
FB.init(appId: env["app_id"], cookie: true)
$('#sign_in').click (e) ->
e.preventDefault()
FB.login (response) ->
window.location = '/auth/facebook/callback' if response.authResponse
$('#sign_out').click (e) ->
FB.getLoginStatus (response) ->
FB.logout() if response.authResponse
true
$(document).on('turbolinks:load', ready)
Run Code Online (Sandbox Code Playgroud)