How do I make this snippet of CoffeeScript TurboLinks 5 friendly?

流过昼夜 提交于 2019-12-11 05:48:32

问题


I have the following:

$ ->
    $('[data-provider="summernote"]').each ->
      $(this).summernote
        height: 300

And this works, however, I would like to make it TurboLinks 5 friendly.

So I tried the following:

$ ->
  ready = ->
    $('data-provider="summernote"]').each ->
      $(this).summernote
        height: 300

  $(document).ready(ready)
  $(document).on('page:load', ready)

But I keep getting this error:

Uncaught ReferenceError: ready is not defined

How do I fix this so that I make the original snippet TL-5 friendly?

Edit 1

By the way, I am doing this in my jobs.coffee file, which I know is being included in my asset-pipeline. So that's not it.


回答1:


In Rails 5 (Turbolinks 5), turbolinks:load. Also, you don't need to wrap your event handlers into the "document ready".

Rewrite your code to this:

ready = ->
  $('data-provider="summernote"]').each ->
    $(this).summernote(height: 300)

$(document).ready(ready)
$(document).on('turbolinks:load', ready)


来源:https://stackoverflow.com/questions/37905668/how-do-i-make-this-snippet-of-coffeescript-turbolinks-5-friendly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!