首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何管理激增的更新路径/消息构造函数?

如何管理激增的更新路径/消息构造函数?
EN

Stack Overflow用户
提问于 2017-05-31 04:35:58
回答 3查看 80关注 0票数 7

我已经完成了Elm指南,并在very simple examples上注意到,update函数增长到3种情况,Msg类型可以有3个构造函数。我想在一个中级项目中,这个数字会增长到20个,而在一个高级项目中,可能会增加到数百个。你怎么做到这一点的?如果每个开发人员都需要为他们的特性添加一个新的构造函数,我预见这将是版本控制争执的一个来源。

我参与了一个react-redux项目,它有一个combining reducers的概念来解决这个问题。我在《榆树》中没有遇到过这个概念。它有一个吗?

EN

回答 3

Stack Overflow用户

发布于 2017-05-31 11:51:58

您可以定义由子/子msg类型组成的msg类型,当然,updater可以与sub函数结合使用。即。

代码语言:javascript
复制
-- Counter


type CounterMsg
    = Increment
    | Decrement


type alias CounterModel =
    Int


updateCounter : CounterMsg -> CounterModel -> ( CounterModel, Cmd msg )
updateCounter msg model =
    case msg of
        Increment ->
            ( model + 1, Cmd.none )

        Decrement ->
            ( model - 1, Cmd.none )



-- Todo


type TodoMsg
    = AddTodo String


type alias TodoModel =
    List String


updateTodo : TodoMsg -> TodoModel -> ( TodoModel, Cmd msg )
updateTodo msg model =
    case msg of
        AddTodo str ->
            ( str :: model, Cmd.none )



-- unified


type alias Model =
    { counter : CounterModel
    , todos : TodoModel
    }


type Msg
    = Counter CounterMsg
    | Todo TodoMsg


initModel =
    { counter = 0, todos = [] }


update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
    case Debug.log "message" msg of
        Counter countermsg ->
            let
                ( newmodel, cmd ) =
                    updateCounter countermsg model.counter
            in
                ( { model | counter = newmodel }, cmd )

        -- etc...
        _ ->
            ( model, Cmd.none )
票数 6
EN

Stack Overflow用户

发布于 2017-05-31 17:12:39

看看Richard's implementation for RealWorld/Conduit吧。它提供了一种现实的方法来构建足够大的应用程序(几千行代码)。

简而言之,在复杂的项目中,页面可以有自己的模型、更新和视图。

在每个页面中,你可以有一个很大的味精,但这不是一个真正的问题。20个标签实际上是相当容易管理的。50也是可管理的,正如NoRedInk程序员在其生产代码中所发现的那样。

票数 1
EN

Stack Overflow用户

发布于 2017-06-15 01:06:20

这里有一个关于这个主题的不错的教程:https://www.elm-tutorial.org/en-v01/02-elm-arch/07-composing-2.html

我希望它能显示Widget的来源,但我可以想象它是什么样子。为子孙后代内联。

代码语言:javascript
复制
module Main exposing (..)

import Html exposing (Html, program)
import Widget


-- MODEL


type alias AppModel =
    { widgetModel : Widget.Model
    }


initialModel : AppModel
initialModel =
    { widgetModel = Widget.initialModel
    }


init : ( AppModel, Cmd Msg )
init =
    ( initialModel, Cmd.none )



-- MESSAGES


type Msg
    = WidgetMsg Widget.Msg



-- VIEW


view : AppModel -> Html Msg
view model =
    Html.div []
        [ Html.map WidgetMsg (Widget.view model.widgetModel)
        ]



-- UPDATE


update : Msg -> AppModel -> ( AppModel, Cmd Msg )
update message model =
    case message of
        WidgetMsg subMsg ->
            let
                ( updatedWidgetModel, widgetCmd ) =
                    Widget.update subMsg model.widgetModel
            in
                ( { model | widgetModel = updatedWidgetModel }, Cmd.map WidgetMsg widgetCmd )



-- SUBSCRIPTIONS


subscriptions : AppModel -> Sub Msg
subscriptions model =
    Sub.none



-- APP


main : Program Never AppModel Msg
main =
    program
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }

我认为这也是https://stackoverflow.com/a/44275318/61624背后的想法,但它有更多的描述。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44271465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档