package module
Version: v0.0.0-...-f4079d8 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2016 License: MIT Imports: 2 Imported by: 0



GopherJS bindings to MithrilJS

Why do this?

Because I can

Mithril.js is a very small and expressive client-side MVC framework. These Go bindings are intended for Developers like me who welcome the strong typing and semantics of Go, and want to use it to easily build great front-end experiences.

Mithril's small API size makes it a great target for defining such a binding. However, it's expressiveness lead to some very JS-centric idioms that are harder to employ in Go.

Who is the target audience?

Me, essentially.

To determine if go-mithril is a good fit for your project, ask if you have:

  • Developers with a strong preference for writing Go over Javascript.
  • Tolerance for slightly lower performance introduced by Gopherjs and having to go through a translation.
  • Developers with at least a passing knowledge of front-end technologies, and finally,
  • Developers with at least a passing knowledge of how mithril itself works.

Perhaps as this project matures, sufficient documentation and abstraction solidity will mean that knowledge of mithril itself will not be necessary.

How mature is this project?

Not very.

This is a very new project. A lot of the methods exposed in mithril.go have not yet been tested, and those that have had not been tested extensively. If you elect to use this project, do so with the understanding that the API is still unstable, and breaking changes have not yet been ruled out.

How do I use it?

Glad you asked.

The most basic bindings are available in the package

For example:

    m.M("a[href='/index.html']", nil, "Home"))

produces the following output

<div id="container">
     <a href="index.html">Home</a>

For a look at a slightly larger example, here is some code that is equivalent to the first example involving rotating links at

This example showcases simple creation of components, views and controllers, making an asynchronous request, and binding a view to a model.


The bindings provided in package mithril do not provide for an idiomatic Go approach to crafting front-end code. For a more pleasant approach, use moria, a set of strongly typed functions, types and interfaces you implement to get the same behaviour.

At present, Moria is only partially implemented.

Can I contribute?

Yes, but...

At the moment, I'm really enjoying lone-wolfing on the project in my spare time. Due to the constraints of professional committments I fear I will not be able to effectively manage contributions in a timely manner. However, contributions will be gratefully accepted as I able.


2016-04-16: Scrapped RenderWithForce in favour of adding the field to render. Loosened some types in Render. Started work on moria, and brought it to enough completeness of features so that the Todo app tutorial could be built.

2016-04-12: The basic bindings to Mithril have been completed. However, with no tests, it's very hard to determine if it is correct.

2016-04-09: With a lot of creakiness and careful conversion between *js.Object and interface{}, the first working example of this library has been launched. It is nowhere near being production ready, however.



Package mithril exports explicit bindings for the Mithril Javascript Library. This version targets v 0.2.3 of the Mithril API. See These bindings assume that the correct Mithril script has already been loaded into the Global namespace before init() is called.



This section is empty.


This section is empty.


func BuildQueryString

func BuildQueryString(object *js.M) string

BuildQueryString serializes an object into its URI encoded querystring representation.

func Component

func Component(component *js.Object, args ...*js.Object) *js.Object

Component initializes a Component object by parametrizing it with args.

func Deferred

func Deferred() *js.Object

Deferred constructs a modified deferred object.

func Deps

func Deps(window *js.Object) *js.Object

Deps is used in testing to replace the default window object on which Mithril depends.

func EndComputation

func EndComputation()

EndComputation is used in conjuction with StartComputation to signal to Mithril when asynchronous work has been completed, and a redraw should happen.

func M

func M(selector string, attrs js.M, children ...interface{}) *js.Object

M composes virtual elements that can be rendered via Render(). The variable parameter children must be either strings or other virtual elements.

func Mount

func Mount(root dom.Node, element interface{}) *js.Object

Mount "hooks up" an element for continuous rendering at a target node.

func ParseQueryString

func ParseQueryString(queryString string) *js.Object

ParseQueryString deserializes an object from its URI encoded querystring representation.

func Prop

func Prop(store *js.Object) *js.Object

Prop creates a getter/setter function. store is the initial value.

func Redraw

func Redraw(force bool)

Redraw requests an aggressive redraw of the view. This redraw happens even if there are pending AJAX requests or other async requests, so make sure templates have null checks in place to account for this. Redraws will not occur if a redraw is currently in progress.

func RedrawStrategy

func RedrawStrategy(strategy ...string) string

RedrawStrategy sets the strategy of redrawing, to either "all", "diff", or "none". Call with one or no arguments.

func Render

func Render(root dom.Node, cell interface{}, force bool)

Render renders a given virtual element cell to a DOM Node. Iff force is true, this will force the recreation of elements.

func Request

func Request(options js.M) *js.Object

Create an asynchronous HTTP request to some url. Returns a deferred prop that can be invoked to get the correct value.

func Route

func Route() string

Route returns the current route. If you're looking for bindings to the other Mithril "route" methods, look at RouteDefine, RouteMode and RouteRedirect. RouteAbstraction has not been implemented so far.

func RouteConfig

func RouteConfig() *js.Object

func RouteDefine

func RouteDefine(rootElement dom.Node, defaultRoute string, routes js.M) *js.Object

RouteDefine allows you to define the routes for a Single-Page Application.

func RouteMode

func RouteMode(mode ...string) string

Property for getting and setting the route mode. Call with 0 or 1 arguments

func RouteParam

func RouteParam(param string) interface{}

func RouteRedirect

func RouteRedirect(path string, params js.M, replaceHistory bool)

RouteRedirect automatically and programmatically allows you to redirect to a given route.

func StartComputation

func StartComputation()

StartComputation is used in conjuction with EndComputation to signal to Mithril when asynchronous work has been completed, and a redraw should happen.

func Sync

func Sync(args ...*js.Object) *js.Object

Sync composes an array of promises into one promise.

func Trust

func Trust(trusted string) *js.Object

Trust annotates a string as trusted, so that HTML entities will not be escaped.

func Version

func Version() string

Version returns the version of the underlying Mithril library.

func WithAttr

func WithAttr(args ...interface{}) *js.Object

WithAttr is an event handler factory. It returns a method that can be bound to a DOM element's event listener, to implement databinding from the view to the model.


This section is empty.

Source Files


Path Synopsis
This package shows the deferred API in action.
This package shows the deferred API in action.
A simple MVC TODO app implemented using Moria.
A simple MVC TODO app implemented using Moria.
Package moria contains idiomatic Go data types and helper functions for defining Mithril components within Go.
Package moria contains idiomatic Go data types and helper functions for defining Mithril components within Go.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL