2016-07-15 7 views
1

祖父母、子コンポーネント、孫コンポーネントがある場合、祖父母は子供の状態を要求できますか?私はhereのような "要求"を使用しようとしましたが、独自の子を持つ子の状態を要求しているときに型が一致しません。子どもがいない子供の状態をリクエストしているとき、このガイドの例はうまくいきます。Purescript Halogen:親でもある子コンポーネントの状態を要求できますか?

エラーは次のとおりです。

Could not match type 

    Query 

with type 

    Coproduct (Coproduct Query (ChildF AnswerSlot Query)) Query 

答えて

1

はい、間違いなく。おそらく子へのクエリにleftがありません。子供のクエリ代数はそれ自身の子を持つCoproduct f (ChildF p f')の形式になります。

これに対応して、祖父母から孫を照会するには、rightを使用し、孫に適切な値を持つChildFを作成します。感謝 -

module Main where 

import Prelude 

import Data.Functor.Coproduct (Coproduct, left, right) 
import Data.Maybe (Maybe(..), fromMaybe) 

import Debug.Trace (traceA) -- from purescript-debug 

import Halogen as H 
import Halogen.HTML as HH 

-------------------------------------------------------------------------------- 

type GrandState = Unit 
data GrandQuery a = AskGrandChild (String -> a) 

grandchild :: forall g. H.Component GrandState GrandQuery g 
grandchild = H.component { render, eval } 
    where 
    render :: GrandState -> H.ComponentHTML GrandQuery 
    render _ = HH.div_ [] 
    eval :: GrandQuery ~> H.ComponentDSL GrandState GrandQuery g 
    eval (AskGrandChild k) = pure $ k "Hello from grandchild" 

-------------------------------------------------------------------------------- 

type ChildState = Unit 
data ChildQuery a = AskChild (String -> a) 
type GrandSlot = Unit 

type ChildState' g = H.ParentState ChildState GrandState ChildQuery GrandQuery g GrandSlot 
type ChildQuery' = Coproduct ChildQuery (H.ChildF GrandSlot GrandQuery) 

child :: forall g. Functor g => H.Component (ChildState' g) ChildQuery' g 
child = H.parentComponent { render, eval, peek: Nothing } 
    where 
    render :: ChildState -> H.ParentHTML GrandState ChildQuery GrandQuery g GrandSlot 
    render _ = HH.slot unit \_ -> { component: grandchild, initialState: unit } 
    eval :: ChildQuery ~> H.ParentDSL ChildState GrandState ChildQuery GrandQuery g GrandSlot 
    eval (AskChild k) = pure $ k "Hello from child" 

-------------------------------------------------------------------------------- 

type ParentState = Unit 
data ParentQuery a = Something a 
type ChildSlot = Unit 

type ParentState' g = H.ParentState ParentState (ChildState' g) ParentQuery ChildQuery' g ChildSlot 
type ParentQuery' = Coproduct ParentQuery (H.ChildF ChildSlot ChildQuery') 

parent :: forall g. Functor g => H.Component (ParentState' g) ParentQuery' g 
parent = H.parentComponent { render, eval, peek: Nothing } 
    where 
    render :: ParentState -> H.ParentHTML (ChildState' g) ParentQuery ChildQuery' g ChildSlot 
    render _ = HH.slot unit \_ -> { component: child, initialState: H.parentState unit } 
    eval :: ParentQuery ~> H.ParentDSL ParentState (ChildState' g) ParentQuery ChildQuery' g ChildSlot 
    eval (Something next) = do 

    -- note the `left` here 
    childAnswer <- H.query unit $ left $ H.request AskChild 
    traceA $ fromMaybe "child not found" $ childAnswer 

    grandAnswer <- H.query unit $ right $ H.ChildF unit $ H.request AskGrandChild 
    traceA $ fromMaybe "grandchild not found" $ grandAnswer 

    pure next 
+0

ああ、このような単純なこと:

私は一緒にうまくいけば、物事をより明確になり、子や孫の両方を照会の不自然な例を入れてきました!私は、クエリ代数がどのように動作するかを理解する時間を取る必要があります。今私はただ貨物を栽培しているだけです。 – sportanova

+0

私は、祖父母が複数のタイプの子供を抱えている例を取り上げました。ここに例がありますhttps://gist.github.com/sportanova/e779fef9f27dbbe1097a064d231c7316 – sportanova

+0

ちょっと、それ 'のための' query''を使用する必要がありますので、 'ChildPath'を指定することができます:https://gist.github.com/garyb/ 73cb5d0c586eb29b319d5859b3747638 –

関連する問題