You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -10,55 +10,6 @@ Elm program is set up using [Browser](https://package.elm-lang.org/packages/elm/
10
10
-`application` - Creates single page application, Elm controls not only the whole document but also Url changes.
11
11
12
12
13
-
## Forms
14
-
15
-
Form elements are created the same way as other HTML elements using functions from [Html](https://package.elm-lang.org/packages/elm/html/latest/Html) module and attributes from [Html.Attributes](https://package.elm-lang.org/packages/elm/html/latest/Html-Attributes) module from [elm/html](https://package.elm-lang.org/packages/elm/html/latest/) package.
16
-
17
-
We can use `onInput` from [Html.Events](https://package.elm-lang.org/packages/elm/html/latest/Html-Events) module to detect input events and create a message for our update function.
18
-
19
-
The loop is the following:
20
-
- user changes the value in an input field
21
-
- a new message is created
22
-
- the update function is called with the message and it updates the model with the new value
23
-
- input field is re-rendered with the new value
24
-
25
-
26
-
Here is a simple example with a single input field:
When we need more complex forms in our application, there are packags to handle forms like [etaque/elm-form](https://package.elm-lang.org/packages/etaque/elm-form/latest/).
60
-
61
-
62
13
## JSON
63
14
64
15
It is very common to use JSON format when communicating with different APIs. In JavaScript, JSON is usually turned into a JavaScript object and used within the application. However, this is not the case in Elm since we have a strong type system. Before we can use JSON data, we need to convert it into a type defined in Elm. There is the [elm/json](https://package.elm-lang.org/packages/elm/json/latest/) package for that.
@@ -81,87 +32,89 @@ For example, we have this JSON representing a TODO:
81
32
To get the `label` field, we can define a decoder like this:
82
33
83
34
```elm
84
-
importJson.DecodeasDexposing (Decoder)
35
+
importJson.DecodeasDecode
85
36
86
-
labelDecoder:DecoderString
37
+
labelDecoder:Decode.DecoderString
87
38
labelDecoder =
88
-
D.field "label"D.string
39
+
Decode.field "label"Decode.string
89
40
```
90
41
91
42
There are functions to decode other primitives, like `bool` or `int`. However, we usually need more than just one field. We can combine decoders using `map` functions from `Json.Decode` module, e.g. `map3`.
92
43
93
44
```elm
45
+
importJson.DecodeasDecode
46
+
94
47
map3:
95
48
(a -> b -> c -> value)
96
-
->Decoder a
97
-
->Decoder b
98
-
->Decoder c
99
-
->Decoder value
49
+
->Decode.Decoder a
50
+
->Decode.Decoder b
51
+
->Decode.Decoder c
52
+
->Decode.Decoder value
100
53
```
101
54
102
55
We can then define our own type for TODO and a decoder.
103
56
104
57
```elm
105
-
importJson.DecodeasDexposing (Decoder)
58
+
importJson.DecodeasDecode
106
59
107
60
type alias Todo=
108
61
{ id :Int
109
62
, label :String
110
63
, completed :Bool
111
64
}
112
65
113
-
todoDecoder:DecoderTodo
66
+
todoDecoder:Decode.DecoderTodo
114
67
todoDecoder =
115
-
D.map3 Todo
116
-
(D.field "id"D.int)
117
-
(D.field "name"D.string)
118
-
(D.field "completed"D.bool)
68
+
Decode.map3 Todo
69
+
(Decode.field "id"Decode.int)
70
+
(Decode.field "name"Decode.string)
71
+
(Decode.field "completed"Decode.bool)
119
72
```
120
73
121
-
There is a package [NoRedInk/elm-json-decode-pipeline](https://package.elm-lang.org/packages/NoRedInk/elm-json-decode-pipeline/latest) for more convenient JSON decoder. It is especially useful for large and more complex objects. We could rewrite the previous example using pipeline:
74
+
There is a package [NoRedInk/elm-json-decode-pipeline](https://package.elm-lang.org/packages/NoRedInk/elm-json-decode-pipeline/latest) for more convenient JSON decoders. It is especially useful for large and more complex objects. We could rewrite the previous example using pipeline:
122
75
123
76
```elm
124
-
importJson.DecodeasDexposing (Decoder)
125
-
importJson.Decode.Pipelineexposing (required)
77
+
importJson.DecodeasDecodeexposing (Decoder)
78
+
importJson.Decode.PipelineasPipeline
126
79
127
80
type alias Todo=
128
81
{ id :Int
129
82
, label :String
130
83
, completed :Bool
131
84
}
132
85
133
-
todoDecoder:DecoderTodo
86
+
todoDecoder:Decode.DecoderTodo
134
87
todoDecoder =
135
-
D.succeed Todo
136
-
|> required "id"D.int
137
-
|> required "name"D.string
138
-
|> required "completed"D.bool
88
+
Decode.succeed Todo
89
+
|>Pipeline.required "id"Decode.int
90
+
|>Pipeline.required "name"Decode.string
91
+
|>Pipeline.required "completed"Decode.bool
139
92
```
140
93
141
94
It is not that big change in this case, however, we only have `map8` function in `Json.Decode` so this library comes handy if we need more. Moreover, it has other functions to define for example [optional](https://package.elm-lang.org/packages/NoRedInk/elm-json-decode-pipeline/latest/Json-Decode-Pipeline#optional) or [hardcoded](https://package.elm-lang.org/packages/NoRedInk/elm-json-decode-pipeline/latest/Json-Decode-Pipeline#hardcoded) values.
142
95
143
96
144
97
### Encoders
145
98
146
-
When we want to send something to an API we need to do the opposite -- turn the Elm value into JSON value. We use functions form[Json.Encode](https://package.elm-lang.org/packages/elm/json/latest/Json-Encode) package for that. There is a type called `Value` which represents a JavaScript value and functions to convert Elm primitives, lists and objects into `Value` type.
99
+
When we want to send something to an API we need to do the opposite -- turn the Elm value into JSON value. We use functions from[Json.Encode](https://package.elm-lang.org/packages/elm/json/latest/Json-Encode) package for that. There is a type called `Value` which represents a JavaScript value and functions to convert Elm primitives, lists and objects into `Value` type.
147
100
148
101
Here's an example using the TODO from decoders example.
149
102
150
103
```elm
151
-
importJson.EncodeasE
104
+
importJson.EncodeasEncode
152
105
153
106
type alias Todo=
154
107
{ id :Int
155
108
, label :String
156
109
, completed :Bool
157
110
}
158
111
159
-
encodeTodo:Todo->E.Value
112
+
encodeTodo:Todo->Encode.Value
160
113
encodeTodo todo =
161
-
E.object
162
-
[("id",E.int todo.id )
163
-
,("label",E.string todo.label )
164
-
,("completed",E.bool todo.completed )
114
+
Encode.object
115
+
[("id",Encode.int todo.id )
116
+
,("label",Encode.string todo.label )
117
+
,("completed",Encode.bool todo.completed )
165
118
]
166
119
```
167
120
@@ -177,7 +130,8 @@ Here is an example for getting TODO using the decoder defined in previous sectio
177
130
```elm
178
131
importHttp
179
132
180
-
type Msg=GotTodo(ResultHttp.ErrorTodo)
133
+
type Msg=
134
+
GotTodo(ResultHttp.ErrorTodo)
181
135
182
136
getTodo:CmdMsg
183
137
getTodo =
@@ -187,7 +141,7 @@ getTodo =
187
141
}
188
142
```
189
143
190
-
The function `getTodo` creates a command with HTTP request that expect JSON to be returned and uses `todoDecoder` to get `Todo` type form the returned JSON. Once the request is finished, we get `GotTodo` message containing the `Result` with either `Http.Error` if the request failed or `Todo` if the request was successful.
144
+
The function `getTodo` creates a command with HTTP request that expect JSON to be returned and uses `todoDecoder` to get `Todo` type from the returned JSON. Once the request is finished, we get `GotTodo` message containing the `Result` with either `Http.Error` if the request failed or `Todo` if the request was successful.
191
145
192
146
There are other functions we can use for expected response like `expectString` to get the string as is or `expectWhatever` when we don't really care about the response as long as it's ok.
193
147
@@ -196,7 +150,8 @@ When we want to do a POST request we also need to define the body. Here's an exa
196
150
```elm
197
151
importHttp
198
152
199
-
type Msg=TodoSaved(ResultHttp.Error())
153
+
type Msg=
154
+
TodoSaved(ResultHttp.Error())
200
155
201
156
postTodo:Todo->CmdMsg
202
157
postTodo todo =
@@ -214,10 +169,10 @@ When we want to do a different type of request than GET and POST or we want to s
214
169
```elm
215
170
request:
216
171
{ method :String
217
-
, headers :ListHeader
172
+
, headers :ListHttp.Header
218
173
, url :String
219
-
, body :Body
220
-
, expect :Expect msg
174
+
, body :Http.Body
175
+
, expect :Http.Expect msg
221
176
, timeout :MaybeFloat
222
177
, tracker :MaybeString
223
178
}
@@ -227,7 +182,7 @@ request :
227
182
228
183
## Subscriptions
229
184
230
-
[Subscirptions](https://package.elm-lang.org/packages/elm/core/latest/Platform-Sub) are used to tell Elm that we want to be informed if something happend (e.g., web socket message or clock tick).
185
+
[Subscriptions](https://package.elm-lang.org/packages/elm/core/latest/Platform-Sub) are used to tell Elm that we want to be informed if something happend (e.g., web socket message or clock tick).
231
186
232
187
233
188
Here's an example of subscriptions defining that a message `Tick` with current time should be send to update function every 1000 milliseconds.
@@ -237,16 +192,88 @@ Here's an example of subscriptions defining that a message `Tick` with current t
237
192
importTime
238
193
239
194
240
-
type Msg=TickTime.Posix
195
+
type alias Model=
196
+
()
197
+
198
+
type Msg=
199
+
TickTime.Posix
241
200
242
201
243
202
subscriptions:Model->SubMsg
244
203
subscriptions model =
245
204
Time.every 1000Tick
246
205
```
247
206
207
+
## Materials
208
+
209
+
-[Examples - TODO List](https://github.com/MI-AFP/elm-examples/tree/master/todo)
-[Elm Europe 2017 - Evan Czaplicki - The life of a file](https://www.youtube.com/watch?v=XpDsk374LDE)
217
+
248
218
249
-
## Random
219
+
### Forms
220
+
221
+
Form elements are created the same way as other HTML elements using functions from [Html](https://package.elm-lang.org/packages/elm/html/latest/Html) module and attributes from [Html.Attributes](https://package.elm-lang.org/packages/elm/html/latest/Html-Attributes) module from [elm/html](https://package.elm-lang.org/packages/elm/html/latest/) package.
222
+
223
+
We can use `onInput` from [Html.Events](https://package.elm-lang.org/packages/elm/html/latest/Html-Events) module to detect input events and create a message for our update function.
224
+
225
+
The loop is the following:
226
+
- user changes the value in an input field
227
+
- a new message is created
228
+
- the update function is called with the message and it updates the model with the new value
229
+
- input field is re-rendered with the new value
230
+
231
+
232
+
Here is a simple example with a single input field:
233
+
234
+
```elm
235
+
importBrowser
236
+
importHtmlexposing (Html)
237
+
importHtml.AttributesasAttributes
238
+
importHtml.EventsasEvents
239
+
240
+
main:Program()ModelMsg
241
+
main =
242
+
Browser.sandbox
243
+
{ init = init
244
+
, update = update
245
+
, view = view
246
+
}
247
+
248
+
type Msg=
249
+
NameChangedString
250
+
251
+
type alias Model=
252
+
{ name :String}
253
+
254
+
init:Model
255
+
init =
256
+
{ name =""}
257
+
258
+
update:Msg->Model->Model
259
+
update msg model =
260
+
case msg of
261
+
NameChanged newName ->
262
+
{ model | name = newName }
263
+
264
+
view:Model->HtmlMsg
265
+
view model =
266
+
Html.input
267
+
[Attributes.placeholder "Your name"
268
+
,Attributes.value model.name
269
+
,Events.onInput NameChanged]
270
+
[]
271
+
```
272
+
273
+
When we need more complex forms in our application, there are packages to handle forms like [etaque/elm-form](https://package.elm-lang.org/packages/etaque/elm-form/latest/).
274
+
275
+
276
+
### Random
250
277
251
278
There is a [Random](https://package.elm-lang.org/packages/elm/random/latest/Random) module in [elm/random](https://package.elm-lang.org/packages/elm/random/latest/) package for generating pseudo-random values in Elm. It defines a type called `Generator` which can be think of as a recipe for generating random values.
252
279
@@ -283,14 +310,4 @@ import Random exposing (Seed)
283
310
generateGrade:Seed-> (Int, Seed)
284
311
generateGrade seed =
285
312
Random.step randomGrade seed
286
-
```
287
-
288
-
## Materials
289
-
290
-
-[Examples - TODO List](https://github.com/MI-AFP/elm-examples/tree/master/todo)
0 commit comments