A simple wiki code. How to define views?


#1

I follow your advice, and, as a beginner, I started my first project: a wiki.
By now each entry of the wiki has just a title, id and a field for knowing if we edit or not this entry. I follow Elm-Todomvc of Evancz but I don’t know how to implement views. Elm-Todomvc does too much for me. I just want to add new entries, delete selected entry.

This is my code (not tested)

-- Imports
import Html exposing (..)

-- Main program
main = Html.program
    { init = init
    , view = view
    , update = update
    , subscriptions = subscriptions
    }


-- Models

type alias Wiki =
  { entries : List Entry
  , name : Maybe String
  , field : String
  , mid : Int
  }


type alias Entry =
  { title : String
  , id : Int
  , editing : Bool
  }


newEntry: String -> Int -> Entry
newEntry title number =
  { title = title
  , id = number
  , editing = False
  }

init: (Wiki, Cmd Msg)
init =
  ( Wiki [] Nothing "" 0, Cmd.none )

-- Update

type Msg
  = NewEntry
  | DeleteEntry Int
  | ModifyTitle Int String

update : Msg -> Wiki -> (Wiki, Cmd Msg)
update msg wiki =
  case msg of
    NewEntry -> { wiki | mid = mid +1
                         , field = ""
                         , if String.isEmpty model.field then
                              wiki.entries
                           else
                             wiki.entries ++ [ newEntry wiki.field wiki.mid ]
                } ! []
    DeleteEntry id -> { wiki | entries = List.filter (\t -> t.id /= id) wiki.entries }
! []

    ModifyTitle id text
         let
                updateEntry t =
                    if t.id == id then
                        { t | title = text }
                    else
                        t
          in
                { wiki | entries = List.map updateEntry wiki.entries }
! []

How to complete the code and write the views and subscriptions?


#2

The view is simply what do you want to show to the user, so the question is: what do you want to show? How do you want to show it?

I recommend starting from the elm tutorial instead of jumping straight to todomvc, which is a “complete” project.


#3

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.