【问题标题】:Unable to bind member_removed pusher event inside binding function of another event无法在另一个事件的绑定函数中绑定 member_removed pusher 事件
【发布时间】:2015-01-19 14:11:48
【问题描述】:

我正在使用 Javascript 订阅频道上的事件,如下所示:

pusher = new Pusher("APP_KEY")
channel = pusher.subscribe('test_channel')

channel.bind('interview', function (data) {
  // do something here

  channel.bind('pusher:member_removed', function (data) {
    console.log("Removed from interview");
  });
});

channel.bind('pusher:member_removed', function (data) {
  console.log('Removed from anywhere');
});

在我的 Rails 操作中,我执行以下操作:

def interview
  Pusher['test_channel'].trigger('interview', {
    interview: 'some-id'
  })
end

我希望在触发该事件时调用两个绑定到pusher:member_removed 的函数。

但是,当它被触发时,只会调用“从任何地方移除”的函数。 “从采访中删除”的功能永远不会执行。

可能出了什么问题?

【问题讨论】:

    标签: javascript ruby-on-rails pusher


    【解决方案1】:

    我很确定 channel.bind 的回调行为不会另外监听其他返回的消息。 Pusher 的人可以详细说明一下吗?

    你可能会做的真正可怕的事情是做类似的事情

    pusher = new Pusher("APP_KEY")
    channel = pusher.subscribe('test_channel')
    var inInterview = false;
    
    channel.bind('interview', function (data) {
      inInterview = true;
    });
    
    channel.bind('pusher:member_removed', function (data) {
      if (inInterview) {
        console.log("Removed from interview");
      } 
      console.log('Removed from anywhere');
    });
    

    【讨论】:

    • 这是否真的很可怕是一个见仁见智的问题。它消除了一些人可能会说是一件好事的嵌套。这也意味着您只需要跟踪一次与事件的绑定。 (注意:我的积压工作中有这个 Q 可以查看).
    • 感谢您的意见 - 如果有更好的方法,请告诉我!
    猜你喜欢
    • 2014-01-20
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多