Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coffee Script not fire on page change, but works on page load. [Rails 5]

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.

here is the script i am using:

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
like image 959
Amrinder Singh Avatar asked Sep 13 '16 04:09

Amrinder Singh


1 Answers

You can try "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)
like image 103
Bartłomiej Gładys Avatar answered Sep 29 '22 01:09

Bartłomiej Gładys