如何使用ActionCable websockets处理js加载器

How to handle js loader with ActionCable websockets?

本文关键字:js 加载 处理 websockets 何使用 ActionCable      更新时间:2023-09-26

所以在我使用Rails 5的应用程序中,我有一个加载器系统,它捕获ajaxSendajaxComplete来动画css中的加载器。

$(document).bind('ajaxSend', () => {
  ...
})
$(document).bind('ajaxComplete', () => {
  ...
})

我的问题很简单,如何用websockets做同样的事情?:)

function connectToYourChannel () {
    // start your animation here
    subscription = App.cable.subscriptions.create("YourChannel") {
        connected: function () {
           // this code will execute when you successfully subscribe
           // to your channel;
           // end your animation here
        }
    }
}

BTW:连接过程非常快。在99%的情况下,你不需要加载消息