2017-08-24 275 views
1

發送初始消息時,無法觸發響應消息。發送初始消息時無法觸發響應消息

我有一個按鈕:

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 

這裏的客戶端代碼榆樹模塊上面描述:

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) 

後市展望:

我希望,當我點擊按鈕,導航就會發生。 但是,沒有任何反應,我不明白爲什麼。

Video

源代碼here

回答

2

顯然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調用,它忽略來自Register.update返回Cmd

(form, _) = 
    Registration.update subMsg model.registration 

這強調具有阻止從孩子update產生的所有命令的效果。您需要保留該子女Cmd,將其映射到父母Cmd,然後在onRegistration個案內返回,而不是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) 

      ... 
+0

我非常感謝您的詳細解答。我該如何回饋? –

0

Cmd.none用於tryRegister函數,它什麼都不做。我想你應該使用Http.send這實際上在http請求完成後觸發消息循環。

最短的例子...,

update msg model = 
    case msg of 
     Submit -> 
      (model, Http.send Response request) 

     Response ... -> 
      ... 
+0

我也有這樣的代碼。但是,我仍然沒有注意到要調用的導航:tryRegister:https://github.com/Lambda-Cartel/Nikeza/blob/master/Client/app/Services/Gateway.elm –

+0

另外,我認爲Cmd.none被映射到一個Cmd Msg,它最終是我作爲tryRegister調用的參數傳入的響應消息? –

+0

下面是我的問題視頻:https://www.youtube.com/watch?v=rdCFDITvW0g –