我想包括zxcvbn.js到我的项目并实现一个密码强度计来配合我的 devise_zxcvbn gem。
但是,我不想通过 Sprockets 加载 zxcvbn.js
。
那么我该如何异步加载它并在加载后的 page:load
期间运行一些代码?
最佳答案
使用回调更新
我可能会使用 getScript 调用,以避免 sprockets 。只是一些 CoffeeScript ,检查有效的设备注册 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
关于javascript - 在 Rails 4 中使用 Sprockets 和 Turbolinks 时如何异步加载 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36531973/