Coffee Script不会在页面更改时触发,但会在页面加载时工作。 [Rails 5] [英] Coffee Script not fire on page change, but works on page load. [Rails 5]

查看:102
本文介绍了Coffee Script不会在页面更改时触发,但会在页面加载时工作。 [Rails 5]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的rails项目中使用咖啡脚本,但问题是它只有当我加载(刷新)页面,而不是当页面呈现,它也应该在页面视图更改工作。

I am using coffee script in my rails project, but the problem is it works only when i load(refresh) the page instead when the page renders, it should also work on the page view change.

这里是我使用的脚本:

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


推荐答案

您可以尝试turoblinks: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)

这篇关于Coffee Script不会在页面更改时触发,但会在页面加载时工作。 [Rails 5]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆