简体   繁体   中英

Javascript conditional in Rails with Turbolinks?

I'm trying to set a conditional for turbolinks pages - where different parts of the page fade depending what part of the site they are on.

I'm using a class in specific links to let me know when I want the alternate behaviour - so the links with class 'commentlink' will trigger a fade of the '.commentBit' div, but the rest of the site will trigger '#mainPage' to fade.

I can't seem to get the following conditional to work, any ideas what I'm doing wrong?

hasBeenClicked = false
$(document).on( 'click', '.commentlink', ->
  hasBeenClicked = true
  )

$(document).on 'page:fetch', ->
  if hasBeenClicked
    $('.commentBit').fadeOut 'fast'
  else
    $('#mainPage').fadeOut 'fast'

If the wrong things are fading out when you click on links, it may be because you are not resetting hasBeenClicked after each fade.

If you click a .commentLink , the .commentBit will properly fade out, but then because hasBeenClicked is still true, any other link click will prompt the same behaviour.

Reset hasBeenClicked to false after fading out .commentBit .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM