如何使用ActionCable websockets处理js loader?

时间:2016-09-18 23:15:25

标签: javascript ruby-on-rails websocket ruby-on-rails-5 actioncable

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

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

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

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

1 个答案:

答案 0 :(得分:0)

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%的情况下,您不会需要加载消息。