使用Opal-jQuery完成事件回调

时间:2021-09-02 00:01:57

I am working on a project that handles multiple jQuery events in succession using the opal-jquery wrapper. jQuery has very effective callback functionality and I am wondering if it is possible to do this in pure ruby with Opal.

我正在开发一个使用opal-jquery包装器连续处理多个jQuery事件的项目。 jQuery具有非常有效的回调功能,我想知道是否有可能在使用Opal的纯ruby中执行此操作。

1 个解决方案

#1


You could use a Promise which is a technique to manage long-term asynchronous events and avoid callback-hell. In regular Opal you would do for example:

您可以使用Promise,这是一种管理长期异步事件并避免回调地狱的技术。在普通蛋白石中你会做的例如:

HTTP.get("url") do |response|
  puts "got response"
end

With promises, this becomes:

随着承诺,这变成:

HTTP.get("url").then do |response|
  puts "got response"
end

The difference lies in the then which returns the Promise (see http://opalrb.org/docs/promises/). The code block will be executed when the HTTP get returns with content (or an error).

区别在于返回Promise的那个(参见http://opalrb.org/docs/promises/)。当HTTP get返回内容(或错误)时,将执行代码块。

Also check up this article on how to use promises with opal-jquery

另请查阅本文,了解如何在opal-jquery中使用promises

http://opalrb.org/blog/2014/05/07/promises-in-opal/

#1


You could use a Promise which is a technique to manage long-term asynchronous events and avoid callback-hell. In regular Opal you would do for example:

您可以使用Promise,这是一种管理长期异步事件并避免回调地狱的技术。在普通蛋白石中你会做的例如:

HTTP.get("url") do |response|
  puts "got response"
end

With promises, this becomes:

随着承诺,这变成:

HTTP.get("url").then do |response|
  puts "got response"
end

The difference lies in the then which returns the Promise (see http://opalrb.org/docs/promises/). The code block will be executed when the HTTP get returns with content (or an error).

区别在于返回Promise的那个(参见http://opalrb.org/docs/promises/)。当HTTP get返回内容(或错误)时,将执行代码块。

Also check up this article on how to use promises with opal-jquery

另请查阅本文,了解如何在opal-jquery中使用promises

http://opalrb.org/blog/2014/05/07/promises-in-opal/