【问题标题】:How to load javascript asynchronously when using Sprockets and Turbolinks in Rails 4在 Rails 4 中使用 Sprockets 和 Turbolinks 时如何异步加载 javascript
【发布时间】:2016-04-10 15:54:35
【问题描述】:

我想将zxcvbn.js 包含到我的项目中,并实现一个密码强度计来配合我的 devise_zxcvbn gem。

但是,我不想通过 Sprockets 加载 zxcvbn.js

那么如何在page:load 期间异步加载它并在加载后运行一些代码?

【问题讨论】:

    标签: javascript ruby-on-rails sprockets turbolinks


    【解决方案1】:

    使用回调更新

    我可能会使用 getScript 调用来避免链轮。只是一些用于检查有效设计注册 url 或模式的咖啡脚本,具体取决于您的设置,然后添加:

    $(document).on 'page:load', ->
      ZxcvbnWithTurbolinks
    
    ZxcvbnWithTurbolinks =
      $.getScript('/js/zxcvbn.js').done((s, Status) ->
        console.warn Status
        # Do some other interesting stuff, like using the script you just fetched
        return
      ).fail (jqxhr, settings, exception) ->
        console.warn 'Something is hinky - not possible to check password strength' + exception
        return
    

    【讨论】:

    • 我如何知道它是否已加载,以便之后我可以实现密码强度栏?
    猜你喜欢
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    相关资源
    最近更新 更多