删除鞋子中的事件

发布于 2024-07-15 12:14:40 字数 101 浏览 7 评论 0原文

有什么办法可以删除鞋子中的事件吗? 我搜索了许多网站和参考资料,但找不到任何删除事件的方法...我介意我需要创建自己的事件管理器...这真的有必要吗? 或者有办法解除事件侦听器的连接?

The is any way to remove the events in Shoes? I searched around many websites and references but I can't found any way to remove a event... I minding that I will need to create my own event manager... its really nescessary? or there is a way to desattach event listeners?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月竹挽风 2024-07-22 12:14:40

我发现 Shoes 只允许对给定事件使用单个侦听器方法,因此您可以通过调用该事件而不向其传递块来删除先前的侦听器。

例如,这个Shoes应用程序在点击一次后就会清除点击事件。 如果您删除块内对 click 的调用,那么它将重复触发。

Shoes.app
  @p = para "Empty"
  @click_count = 0
  click do |b,x,y| 
    @click_count += 1
    @p.replace "Clicked #{@click_count} time(s)."
    click # remove the click handler
  end
end

I have found that Shoes only allows a single listener method on a given event, so you can remove a previous listener by calling the event and not passing a block to it.

For example, this Shoes app will clear the click event after it is clicked one time. If you remove the call to click inside the block, then it will fire repeatedly.

Shoes.app
  @p = para "Empty"
  @click_count = 0
  click do |b,x,y| 
    @click_count += 1
    @p.replace "Clicked #{@click_count} time(s)."
    click # remove the click handler
  end
end
花开柳相依 2024-07-22 12:14:40

您要删除哪些键绑定? Shoes 应用程序上的所有事件还是仅默认绑定?

如果您想覆盖为鞋子保留的绑定,例如“alt-/”、“alt-.”、“alt-?”,请将以下代码粘贴到包含您的应用程序代码的文件中。

class Shoes::App
  def Shoes.show_log # gets called on alt-/
  end

  def Shoes.show_manual # gets called on alt-?
  end

  def Shoes.show_selector # gets called on alt-.
  end
end

上面的代码对鞋子代码进行了猴子修补,并当按下相应的键时,又不执行任何操作。

您可以对其余默认绑定使用相同的技术。 grep 鞋源中的按键绑定,找到相应的方法并在应用程序中定义一个空方法来覆盖内置方法。

Which keybindings do you want to remove? All events on the Shoes app or just the default bindings?

If you want to override bindings reserved for shoes like "alt-/", "alt-.", "alt-?", paste the following code into the file which contains your application code

class Shoes::App
  def Shoes.show_log # gets called on alt-/
  end

  def Shoes.show_manual # gets called on alt-?
  end

  def Shoes.show_selector # gets called on alt-.
  end
end

The above code monkey-patches the shoes code and in-turn does nothing when the corresponding keys are pressed.

You can use the same technique for rest of the default bindings. grep the shoes source for the key bindings, find the corresponding method and define an empty method within your app to override the built-in method.

伪心 2024-07-22 12:14:40

您是否尝试过 method.unbind(obj)

Have you tried method.unbind(obj)?

忆离笙 2024-07-22 12:14:40

您能澄清一下“删除事件”的含义吗? 您想忽略特定事件还是想“关闭”事件侦听器?

如果是前者,我建议编写一个忽略该事件的监听器。

如果是后者,为什么不让侦听器的主体以某些外部可访问的值为条件,给自己一个开/关开关。

如果您想要其他内容,请编辑问题以澄清,我稍后会停下来编辑我的答案。

为了回应您的评论,我会重新建议上述替代方案中的第二个。 如果你想变得非常奇特,你可以写这样的东西:

$keypress_listeners = {}
keypress do |key|
    $keypress_listeners.values.each { |l| l.call(key)
    end
$keypress_listeners[:hero_controller] = lambda { |key| ... }
:
:
$keypress_listeners.delete[:hero_controller]

对于任何其他事件也是如此,但这可能有点矫枉过正。 另一方面,它可以让您完全控制事件处理。

Can you clarify what you mean by "remove a event"? Are you wanting to ignore a specific event or are you wanting to "turn off" an event listener?

If the former, I'd suggest writing a listener that just ignore the event.

If the later, why not make the body of the listener conditional on some externally accessible value, giving yourself an on/off switch.

If you are wanting something else, edit the question to clarify and I'll stop back later and edit my answer.

In response to your comment, I'd re-suggest the second of the above alternatives. If you wanted to get really fancy you could write something like this:

$keypress_listeners = {}
keypress do |key|
    $keypress_listeners.values.each { |l| l.call(key)
    end
$keypress_listeners[:hero_controller] = lambda { |key| ... }
:
:
$keypress_listeners.delete[:hero_controller]

and likewise for any other events, but that is probably overkill. On the other hand, it would give you total control of the event processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文