ocaml-vdom: Elm architecture and (V)DOM for OCaml

Build Status

Overview

This package contains:

Dependencies

Installation (with OPAM)

opam install ocaml-vdom

Manual installation

git clone https://github.com/LexiFi/ocaml-vdom.git
cd ocaml-vdom
make all
make doc
make examples   # Optional (browse index.html files in _build/default/examples to try out)
make install

DOM bindings

Js_browser exposes (partial) OCaml bindings of the browser's DOM and other common client-side Javascript APIs.

It is implemented with gen_js_api, making it realistic to have it working with Bucklescript in the future. This would open the door to writing client-side web applications in OCaml that could be compiled to Javascript either with js_of_ocaml or Bucklescript.

VDOM

The Elm architecture is a functional way to describe UI applications. In this architecture, the current state of the UI is represented with a single data type and a "view" function projects this state to a concrete rendering. In our case, this rendering is done to a tree-like abstraction of the browser DOM, called a VDOM (Virtual DOM). This VDOM can itself be rendered to a concrete DOM. Whenever the state changes, the view function produces a new VDOM tree, which is then diffed with the previous one to update the concrete DOM accordingly. The VDOM also specifies how DOM events are wrapped into "messages" that are processed by an "update" function to modify the current state. This function can also spawn "commands" (such as AJAX calls) whose outcome is also notified by messages.

The implementation of this architecture relies on two modules:

This implementation of VDOM has some specificities:

Usage

A simple one-module application would look like:

open Vdom

(* Definition of the vdom application *)

type model = .... (* the state of the application *)
let view model =  ...  (* the state->vdom rendering function *)
let init = return ... (* the initial state *)
let update model = function .... (* the state-updating function *)
let my_app = app ~init ~update ~view ()


(* Driver *)

open Js_browser

let run () =
  Vdom_blit.run my_app   (* run the application *)
  |> Vdom_blit.dom    (* get its root DOM container *)
  |> Element.append_child (Document.body document)   (* insert the DOM in the document *)

let () = Window.set_onload window run

Compiling this to Javascript:

    ocamlfind ocamlc -package ocaml-vdom -no-check-prims -linkpkg -o myprog.exe myprog.ml
js_of_ocaml +gen_js_api/ojs_runtime.js -o myprog.js myprog.exe

The Javascript code can then be used from a simple HTML file such as:

<html>
  <head>
    <script src="myprog.js"></script>
  </head>
  <body>
  </body>
</html>

Examples: Demo, Counters

Third-party examples: TodoMVC (source, demo), With Eliom service

About

This project has been created by LexiFi initially for its internal use. It is already used in production but it is still relatively new and no commitment is made on the stability of its interface. So please let us know if you consider using it!

This ocaml-vdom package is licensed by LexiFi under the terms of the MIT license.

Contact: alain.frisch@lexifi.com