发送初始消息时无法触发响应消息
Unable to trigger response message when sending initial message
发送初始消息时无法触发响应消息。
我有一个按钮:
button
[ class "register"
, value "Create Account"
, onClick Submit
]
我有以下消息:
type Msg
= Submit
| Response (Result Http.Error JsonProfile)
通过单击按钮调用的消息处理程序如下:
update : Msg -> Form -> ( Form, Cmd Msg )
update msg model =
case msg of
Submit ->
( model, runtime.tryRegister model Response )
...
这是其他消息处理程序:
update : Msg -> Form -> ( Form, Cmd Msg )
update msg model =
case msg of
Submit ->
( model, runtime.tryRegister model Response )
Response (Ok json) ->
( model, Navigation.load <| "/#/portal/1" )
Response (Err error) ->
( model, Cmd.none )
我的 tryRegister 实现如下:
tryRegister : Form -> (Result Http.Error JsonProfile -> msg) -> Cmd msg
tryRegister form msg =
let
jsonProfile =
JsonProfile 1 form.firstName form.lastName form.email
newMsg v =
msg
in
Cmd.map (newMsg <| Result.Ok jsonProfile) Cmd.none
这是上面描述的 elm 模块的客户端代码:
onRegistration : Registration.Msg -> Model -> ( Model, Cmd Msg )
onRegistration subMsg model =
let
( form, _ ) =
Registration.update subMsg model.registration
in
case subMsg of
Registration.Submit ->
( { model | registration = form }, Cmd.none )
Registration.Response result ->
case result of
Result.Ok jsonProfile ->
let
newUser =
jsonProfileToProvider jsonProfile
newState =
{ model
| registration = form
, portal =
{ initPortal
| provider = newUser
, requested = Domain.EditProfile
, linksNavigation = False
, sourcesNavigation = False
}
}
in
( newState, Navigation.load <| "/#/portal/" ++ getId newUser.profile.id )
Result.Err _ ->
( model, Cmd.none )
期望:
我希望在单击按钮时进行导航。
但是,没有任何反应,我不明白为什么。
源代码是here.
Cmd.none
用于tryRegister
函数,什么都不做。我认为您应该使用 Http.send
,它实际上会在 http 请求完成后触发消息循环。
最短的例子...,
update msg model =
case msg of
Submit ->
(model, Http.send Response request)
Response ... ->
...
显然 Cmd.map (...) Cmd.none
不足以强制另一个更新周期。您可以通过使用 Task.perform
.
发送始终成功的任务来强制更新周期
tryRegister : Form -> (Result Http.Error JsonProfile -> msg) -> Cmd msg
tryRegister form msg =
JsonProfile 1 form.firstName form.lastName form.email
|> Result.Ok
|> msg
|> Task.succeed
|> Task.perform identity
注意:有充分的理由不这样做,如 outlined here,但我们暂时忽略这些以适应您概述的框架
但是,仅此一项并不能使您的代码正常工作。您有一个嵌套的 update
调用忽略了 Cmd
return 来自 Register.update
:
( form, _ ) =
Registration.update subMsg model.registration
该下划线具有阻止从子 update
生成的所有命令的效果。您将需要保留该子 Cmd
,将其映射到父 Cmd
,并在所有 onRegistration
情况下将其 return 而不是 Cmd.none
。例如:
onRegistration : Registration.Msg -> Model -> ( Model, Cmd Msg )
onRegistration subMsg model =
let
( form, subcmd ) =
Registration.update subMsg model.registration
regcmd =
Cmd.map OnRegistration subcmd
in
case subMsg of
Registration.FirstNameInput _ ->
( { model | registration = form }, regcmd )
...
发送初始消息时无法触发响应消息。
我有一个按钮:
button
[ class "register"
, value "Create Account"
, onClick Submit
]
我有以下消息:
type Msg
= Submit
| Response (Result Http.Error JsonProfile)
通过单击按钮调用的消息处理程序如下:
update : Msg -> Form -> ( Form, Cmd Msg )
update msg model =
case msg of
Submit ->
( model, runtime.tryRegister model Response )
...
这是其他消息处理程序:
update : Msg -> Form -> ( Form, Cmd Msg )
update msg model =
case msg of
Submit ->
( model, runtime.tryRegister model Response )
Response (Ok json) ->
( model, Navigation.load <| "/#/portal/1" )
Response (Err error) ->
( model, Cmd.none )
我的 tryRegister 实现如下:
tryRegister : Form -> (Result Http.Error JsonProfile -> msg) -> Cmd msg
tryRegister form msg =
let
jsonProfile =
JsonProfile 1 form.firstName form.lastName form.email
newMsg v =
msg
in
Cmd.map (newMsg <| Result.Ok jsonProfile) Cmd.none
这是上面描述的 elm 模块的客户端代码:
onRegistration : Registration.Msg -> Model -> ( Model, Cmd Msg )
onRegistration subMsg model =
let
( form, _ ) =
Registration.update subMsg model.registration
in
case subMsg of
Registration.Submit ->
( { model | registration = form }, Cmd.none )
Registration.Response result ->
case result of
Result.Ok jsonProfile ->
let
newUser =
jsonProfileToProvider jsonProfile
newState =
{ model
| registration = form
, portal =
{ initPortal
| provider = newUser
, requested = Domain.EditProfile
, linksNavigation = False
, sourcesNavigation = False
}
}
in
( newState, Navigation.load <| "/#/portal/" ++ getId newUser.profile.id )
Result.Err _ ->
( model, Cmd.none )
期望:
我希望在单击按钮时进行导航。 但是,没有任何反应,我不明白为什么。
源代码是here.
Cmd.none
用于tryRegister
函数,什么都不做。我认为您应该使用 Http.send
,它实际上会在 http 请求完成后触发消息循环。
最短的例子...,
update msg model =
case msg of
Submit ->
(model, Http.send Response request)
Response ... ->
...
显然 Cmd.map (...) Cmd.none
不足以强制另一个更新周期。您可以通过使用 Task.perform
.
tryRegister : Form -> (Result Http.Error JsonProfile -> msg) -> Cmd msg
tryRegister form msg =
JsonProfile 1 form.firstName form.lastName form.email
|> Result.Ok
|> msg
|> Task.succeed
|> Task.perform identity
注意:有充分的理由不这样做,如 outlined here,但我们暂时忽略这些以适应您概述的框架
但是,仅此一项并不能使您的代码正常工作。您有一个嵌套的 update
调用忽略了 Cmd
return 来自 Register.update
:
( form, _ ) =
Registration.update subMsg model.registration
该下划线具有阻止从子 update
生成的所有命令的效果。您将需要保留该子 Cmd
,将其映射到父 Cmd
,并在所有 onRegistration
情况下将其 return 而不是 Cmd.none
。例如:
onRegistration : Registration.Msg -> Model -> ( Model, Cmd Msg )
onRegistration subMsg model =
let
( form, subcmd ) =
Registration.update subMsg model.registration
regcmd =
Cmd.map OnRegistration subcmd
in
case subMsg of
Registration.FirstNameInput _ ->
( { model | registration = form }, regcmd )
...