如何在超栈中使用 HTTP

How to use HTTP in hyperstack

我已经使用 hyperstack.org 的安装说明对 hyperstack rails 应用程序进行了基本安装,尝试在 after_mount 回调中添加 HTTP.get 请求。

不太确定我还能尝试什么,认为 HTTP 将是一个标准选项

class App < HyperComponent
  include Hyperstack::Router

  after_mount do
    HTTP.get('/example.json')
  end

  render do
    DIV() do
      'App'
      # NodeDisplay
      # define routes using the Route psuedo component.  Examples:
      # Route('/foo', mounts: Foo)                : match the path beginning with /foo and mount component Foo here
      # Route('/foo') { Foo(...) }                : display the contents of the block
      # Route('/', exact: true, mounts: Home)     : match the exact path / and mount the Home component
      # Route('/user/:id/name', mounts: UserName) : path segments beginning with a colon will be captured in the match param
      # see the hyper-router gem documentation for more details
    end
  end
end

收到的错误是:

Uncaught error: HTTP: uninitialized constant App::HTTP
in App (created by Hyperstack::Internal::Component::TopLevelRailsComponent)
in Hyperstack::Internal::Component::TopLevelRailsComponent

简单回答:Opal​​ 或 Hyperstack 默认不包含 HTTP 库。

您可以将它作为 Opal jQuery 包装器的一部分,或者与最小的 Opal Browser::HTTP 库一起包含。

要将 jQuery 包装器添加到您的 Hyperstack 应用程序,请执行以下操作:

  1. Import the Hypestack jquery wrapper by adding
    import 'hyperstack/component/jquery', client_only: true
    to your config/initializers/hyperstack.rb file.

  2. Then include the actual jquery javascript code in your assets:

    If using webpacker run yarn add jquery in your terminal, and then add this line to the javascripts/packs/client_only.js file: jQuery = require('jquery');

    If not using webpacker instead add import 'jquery', client_only: true to the hyperstack initializer file.

如果您只想使用更精简的 Browser::HTTP 模块,请添加

import 'browser/http

to your config/initializers/hyperstack.rb file.

After changing your hyperstack.rb you will have to clear the rails tmp cache by running rm -rf tmp/cache

Note: When using the browser version you will need to use Browser::HTTP instead of simply HTTP.