标签: elm-architecture

如何在elm架构中处理多个订阅

我正在通过榆树指南.

在效果子章节中,有一个带有时间订阅的示例

subscriptions : Model -> Sub Msg
subscriptions model =
  Time.every second Tick
Run Code Online (Sandbox Code Playgroud)

以及处理Web-Sockets-subscriptions的示例

subscriptions : Model -> Sub Msg
subscriptions model =
  WebSocket.listen "ws://echo.websocket.org" NewMessage
Run Code Online (Sandbox Code Playgroud)

但在这些示例中,只有一个订阅.我怎么能处理多个订阅?

elm elm-architecture

13
推荐指数
1
解决办法
1304
查看次数

榆树 - 将Msg变成Cmd Msg

我正在尝试从elm-lang教程修改一个简单的应用程序,首先更新模型,然后触发另一个更新.

update msg model =
  case msg of
    MorePlease ->
      (model, getRandomGif model.topic)

    NewGif (Ok newUrl) ->
      ( { model | gifUrl = newUrl }, Cmd.none)

    NewGif (Err _) ->
      (model, Cmd.none)

    -- my addition
    NewTopic newTopic ->
      ({ model | topic = newTopic}, MorePlease)
Run Code Online (Sandbox Code Playgroud)

这在编译器中失败,因为NewTopic分支:

The 3rd branch has this type:

( { gifUrl : String, topic : String }, Cmd Msg )

But the 4th is:

( { gifUrl : String, topic : String }, Msg …
Run Code Online (Sandbox Code Playgroud)

elm elm-architecture

12
推荐指数
2
解决办法
2900
查看次数

为什么榆树建筑被称为TEA?

TEA在网上的许多地方都看到了这个缩写词(?!) - 意思是榆树建筑 - 但我不明白每个初始代表什么.谢谢.

elm elm-architecture

5
推荐指数
1
解决办法
425
查看次数

如何在Elm中将`Msg`类型分成多种类型?

在Elm中使用模型和更新的标准方法是定义Model和Msg类型以及更新函数:

type alias Model = { ... }

type Msg = Msg1 | Msg2 | ...

update : Msg -> Model -> (Model, Cmd Msg)
...
Run Code Online (Sandbox Code Playgroud)

当应用程序增长时,所有这些类型和功能变得更加复杂.我想以下列方式将它们分开:

type alias Model1 = { ... }
type alias Model2 = { ... }
type alias Model = { model1 : Model1, model2 : Model2 }

type Msg1 = Msg1a | Msg1b | ...
type Msg2 = Msg2a | Msg2b | ...
type Msg = M1 Msg1 | M2 Msg2 | ...
Run Code Online (Sandbox Code Playgroud)

然后,我想分别处理所有这些(我知道该怎么做). …

elm elm-architecture

5
推荐指数
1
解决办法
86
查看次数

在Elm中序列Http.get

下面我有一个button试图加载远程内容...

import Post exposing (Post)
import Html exposing (..)
import Html.Events exposing (..)
import Http
import Json.Decode as Decode


type alias Model =
    { posts : List Post }


type Msg
    = Search String
    | PostsReceived (Result Http.Error (List Post))


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        Search s ->
            let
                cmd =
                    (Decode.list Post.decode)
                        |> Http.get ("/posts?author=" ++ s)
                        |> Http.send PostsReceived
            in
                ( model, cmd …
Run Code Online (Sandbox Code Playgroud)

elm elm-architecture

4
推荐指数
1
解决办法
278
查看次数

在Elm Architecture中编写程序

假设我想创建一个包含两个组件的网页,比如a Navbar和a Body.这两个组件不相互作用,可以独立开发.所以,我有两个榆树文件,每个文件都有以下组件:

type Model = ...

type Msg = ...

init : (Model, Cmd Msg)

update : Msg -> Model -> (Model, Cmd Msg)

view : Model -> Html Msg
Run Code Online (Sandbox Code Playgroud)

假设它们都工作正常,我们如何组合它们来制作一个包含这两个组件的程序?

我试着写这样的东西:

type Model = {body : Body.Model , navbar : Navbar.Model}
type Msg = BodyMsg Body.Msg | NavbarMsg Navbar.Msg

view : Model -> Html Msg
view model = div [] [Body.view model.body, Navbar.view model.navbar]

update : Msg -> Model -> (Model, Cmd Msg)
update = ... …
Run Code Online (Sandbox Code Playgroud)

composition elm elm-architecture

4
推荐指数
2
解决办法
353
查看次数

处理 elm 中的大量输入字段是否有一个很好的模式?

elm 中是否有一种模式可以避免编写大量消息只是为了更新模型子元素上的各个字段?

目前,我最终得到如下代码,每个更改的输入都有一条消息,然后是每个字段的一堆更新逻辑。我想要做的是有一个像 AChanged 这样的消息来处理对 A 的任何属性的所有更改。要么通过更新生成消息的函数中的记录,要么传入一个字段名,然后使用它来直接执行像在 Javascript 中一样更新记录。

module Main exposing (Model)

import Browser exposing (Document, UrlRequest)
import Browser.Navigation as Nav exposing (Key)
import Html exposing (div, input)
import Html.Events exposing (onInput)
import Url exposing (Url)


type alias A =
    { a : String
    , b : String
    , c : String
    , d : String
    }


type alias B =
    { e : String
    , f : String
    , g : String
    , h : String
    }


type …
Run Code Online (Sandbox Code Playgroud)

elm elm-architecture

4
推荐指数
1
解决办法
297
查看次数

Type Mismatch - 1st argument to sandbox is not what I expect

I am trying to add subscriptions as I have a dropdown, this helps ensure that the dropdowns automatically close when you click outside of them. On doing so, I had to change the model as well as my update.

This link (will take you to the Elm Bootstrap site) is the dropdown I am working with which is using Bootstrap 4.

Error I am getting

The 1st argument to sandbox is not what I expect:

295| Browser.sandbox 296|> { …

typeerror elm elm-architecture

4
推荐指数
1
解决办法
120
查看次数

如何从Elm中的组件调用父msg?

我有一个模态窗口,可以在其中显示不同的组件.每个组件都有自己的更新程序和消息,但我想在它们之间共享一个关闭按钮.

因此我不能直接从我的孩子那里叫"CloseModal" - 榆树不允许我给别人打电话.我有什么选择?


我以为我可以调用"Modal.Update.update Modal.Messages.CloseModal",但在我的组件中我只有一个状态的块.所以这不是一个选择.

然后我找到了一种将消息从父节点传递给子节点的方法,但它无法帮助我以其他方式传递消息.或兄弟姐妹.

elm elm-architecture

3
推荐指数
1
解决办法
1277
查看次数

`Cmd msg`是什么意思?

我正在尝试使用端口将URL传递给Javascript,以便将用户重定向到另一个页面.我写了一个port module包含我的项目所需的所有端口:

port module Utils exposing (..)
port changePage : String -> Cmd Event
Run Code Online (Sandbox Code Playgroud)

然后,我在我的Elm文件中导入它:

type Event = PageChange String
import Utils exposing (changePage)
Run Code Online (Sandbox Code Playgroud)

但是编译器不喜欢它:

It looks like the keyword `import` is being used as a variable.
8| import Utils exposing (changePage)
         ^
Rename it to something else.
Run Code Online (Sandbox Code Playgroud)

所以我将端口定义移动到主文件:

type Event = PageChange String
port changePage : String -> Cmd Event
Run Code Online (Sandbox Code Playgroud)

但是编译器仍然不同意:

Port `changePage` has an invalid type.
28| port changePage : String -> Cmd Event
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You …
Run Code Online (Sandbox Code Playgroud)

elm elm-architecture

3
推荐指数
2
解决办法
979
查看次数

什么是`init:()->(Model,Cmd Msg)`注释?

Elm官方网站上有一个init我不理解的函数定义:

init : () -> (Model, Cmd Msg)
init _ =
  ( Loading
  , Http.get
      { url = "https://elm-lang.org/assets/public-opinion.txt"
      , expect = Http.expectString GotText
      }
  )
Run Code Online (Sandbox Code Playgroud)

因此,init函数返回一个tuple,然后为什么不将其符号定义为:

init: (Model, Cmd Msg)
Run Code Online (Sandbox Code Playgroud)

但是在他们的示例中init返回的函数返回一个tuple。这是真的?

以及如何阅读()init : () -> (Model, Cmd Msg)?例如,我可以说从任何东西返回元组吗?

types elm elm-architecture

3
推荐指数
1
解决办法
78
查看次数

标签 统计

elm ×11

elm-architecture ×11

composition ×1

typeerror ×1

types ×1