kopiert ich diesen Code, so dass jeder es ausprobieren kann http://elm-lang.org/tryElm - Aktualisierung Felder dynamisch in Elm
import Html exposing (..)
import Html.App as App
import Html.Attributes exposing (..)
import Html.Events exposing (..)
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}
init =
({ posts = [], newPost = { title = "", slug = "" } }, Cmd.none)
type alias Post =
{ title : String
, slug : String
}
type alias Model =
{ posts : List Post
, newPost : Post
}
type Msg
= NoOp
| NewPostField Post String String
update msg model =
case msg of
NoOp ->
(model, Cmd.none)
NewPostField currentPost field newValue ->
case field of
"title" ->
({ model | newPost = { slug = currentPost.slug, title = newValue } }, Cmd.none)
"slug" ->
({ model | newPost = { slug = newValue, title = currentPost.title } }, Cmd.none)
-- The problem is here, I have to repeat this process for every field
_ ->
(model, Cmd.none)
view model =
div []
[ h1 [] [ text ("title : " ++ model.newPost.title ++ " | slug : " ++ model.newPost.slug) ]
, input [ onInput (NewPostField model.newPost "title"), placeholder "Title" ] []
, input [ onInput (NewPostField model.newPost "slug"), placeholder "Slug" ] []
, button [] [ text "Save" ]
]
I minimiert, die Felder zu zwei (Titel und Wegschnecke), aber es gibt andere wie: Inhalt, Auszug. ..
Gibt es trotzdem, ich kann den Datensatz aktualisieren, ohne einen Fall für alle Felder zu erstellen, etwas wie die Aktualisierung nur das Feld notwendig, ohne durch alle von ihnen zu gehen?
Ja ich dies zunächst tat, ich bin auf der Suche nach effizienteren Weg, anstatt eine Funktion Update für jedes Feld, (ich habe einige Modelle, die mehr als 12 Felder hat) – kayne
Ich mag diese Antwort als Elm-Datensatz ist ein statischer Typ. Wenn ich etwas Dynamisches haben möchte, dann würde ich 'Dict' oder so etwas verwenden. – Tosh
Danke Jungs, ich denke, das ist der Weg in Elm, zu viel schreiben, ich bleibe dabei – kayne