DEV Community

Discussion on: Modelling a save button in Elm

 
yadalis profile image
Suresh Yadali

Awesome, I got it working with this code. Here is the ellie link ellie-app.com/3jDVmS9GVq3a1 thanks for the help. Also, just wondering about "Blog.encode", is encode is available by default in any type?

Thread Thread
 
jwoudenberg profile image
Jasper Woudenberg • Edited

Hey, this is great! Thanks for sharing!

With regards to Blog.encode, the Blog in there refers to the module called Blog, not the type Blog (confusing, I know). So Blog.encode refers to the encode function in the Blog module. In Elm types never have 'methods', like you would see in an object oriented language, so when you see CapitalizedThing.anything you can be sure CapitalizedThing is referring to a module.

When a type has a bunch of helper functions doing stuff with it, these are often put together in a module named after the type. In my post I hypothesize such a module Blog exists, through the line import Blog exposing (Blog) in the example, but don't show the implementation of that module. The encode function defined in there might look something like this:

import Json.Encode exposing (Value)

encode : Blog -> Value
encode (Blog blog) = Json.Encode.string blog

Json.Encode comes form the elm/json package.

Thread Thread
 
yadalis profile image
Suresh Yadali

Wonderful. Understood your explanation and totally make sense now :).
Thank you very much for taking the time to help me out. I am getting my head around it quite nicely on Elm language. Elm rocks!!!

Also, I have a different version of CHANGE function since I am not sure what I would gain by passing a function and a blog, so tried the below function and it works, but not sure if I am missing any Elm standards here, but just a different approach.

buildUpdatedBlog : doc -> MaybeSaved doc -> MaybeSaved doc
buildUpdatedBlog newBlog (MaybeSaved lastSaved current) =
MaybeSaved lastSaved newBlog

here is the latest ellie link, ellie-app.com/3jHs2wFCmhXa1, any suggestions would be great.

Hope to see you at Elm Conf 2018.

Thread Thread
 
jwoudenberg profile image
Jasper Woudenberg

Looking great!

Also, I have a different version of CHANGE function since I am not sure what I would gain by passing a function and a blog.

Yeah, that's a good simplification!

The version of change in the post only comes in handy when you want to make a change that somehow depends on the previous value. Because the onInput handler for our text area always gives us the entire text of the blog we're not really interested in the old value of the blog. But if your blog app will for example have separate inputs for the blog title and the blog body the version of change in my post will come in handy.

Hope to see you at Elm Conf 2018.

I have to miss it unfortunately, I won't be in the US. Hope you enjoy it!