Hello everyone. I’m making my first elm modules that are going to be re-used across different pages. I wanted to follow Evan’s API design of sortable-table, but is hard for me to think a clear way on how to separate the view and the data, and there is not a lot of information on internet (or I haven’t found) on how to think about designing modules this way.
I want to make a Filters Module. Right now I have everything in the Model and I don’t know if is good/wrong and if I should split some parts from model and just pass it to the view like this?
module DataTable.Filter exposing ...
type Model
= Model Internals
type alias Internals =
{ expanded : Bool
, filters : OrderedDict String Filter
, currentDate : Date
}
type Filter
= ValueFilter FilterableByValue
| RangeFilter FilterableByRange
init : List ( String, Filter ) -> ( Model, Cmd Msg )
init filters =
( Model <|
{ expanded = False
, filters = OrderedDict.fromList filters
, currentDate = initialDate
}
, Task.perform GotCurrentDate Date.today
)
view : Model -> Html Msg
view (Model internals) =
div [ class "row" ]
[ ... ]
update : Msg -> Model -> ( Model, ExternalMsg )
update msg (Model internals) =
case msg of
...
I used it in other pages like this:
type alias Model =
{ session : Session
...
, filters : Filter.Model
, pagination : Pagination.Model
}
init : Session -> ( Model, Cmd Msg )
init session =
let
( filterModel, filterCmd ) =
Filter.init
[ Filter.byText "Account Number"
...
, Filter.byDate "Created At"
|> Filter.withDynamicType
]
dataTable =
DataTable.init
pagination =
Pagination.init
in
( { session = session
...
, filters = filterModel
, pagination = pagination
}
, Cmd.batch
[ Cmd.map FilterMsg filterCmd
-- TODO: Load Bills
]
)
view : Model -> { title : String, content : Html Msg }
view model =
{ title = "Bills"
, content =
case Session.viewer model.session of
Nothing ->
text "Sign in to view the Bills."
_ ->
div [ class "dataTables_wrapper" ]
[ -- Filters
Html.map FilterMsg <| lazy Filter.view model.filters
, -- DataTable
viewDataTable model.bills model.dataTable
, -- Pagination
if RemoteData.isSuccess model.bills then
Pagination.view False model.pagination
else
Pagination.view True model.pagination
]
}
I can post more information about the module if needed. I have also another module called DataTable.Pagination
and I have the same question as Filters module