Giter Site home page Giter Site logo

mithril-utils's Introduction

mithril-utils

Usage | API

Utilities for Mithril

Usage

Simple Usage

For simple useage, import the hole utilities bundle via require import.

var m = require('mithril')
var mu = require('mithril-utils')

var div = m( "div", mu.attrs.onclick( executeMeWhenDivClicked ) )

To reduce dependencies, you can import modules the direct way.

var m = require('mithril')
var onclick = require('mithril-utils/attrs/onclick')

var div = m( "div", onclick( executeMeWhenDivClicked ) )

Create CSS Class Attribute

var toClass = require("mithril-utils/attrs/toClass")
var div = m( "div", toClass( {
  "highlight": ctrl.isHighlighted(),
  "bold": ctrl.isBold(),
  "italic":  ctrl.isItalic()
  } ) )

// when ctrl.isHighlighted() == true
// when ctrl.isBold() == false
// when ctrl.isItalic() == true
// will create { 'class': 'highlight italic' }

Create Event bi-directional binding

m( "input", mu.attrs.onchange( model.description ) )

API

Attrs module bundle

The attrs module bundles functionality to create the attributes object for the mithril m function

Object attrs.toClass( Object css [Object prevGenAttr] )

The toClass function will create from a give object like this:

var css = {
  "highlight": isHighlighted(),
  "bold": isBold(),
  "italic":  isItalic()
}

When the evaluation of the values are like this:

isHighlighted() == true
isBold() == false
isItalic() == true

It returnd an object that can be used to stile that div as shown:

m( "div", { 'class': 'highlight italic' } )

Object attrs.onclick( Function fn [,Object prevGenAttr] )

Creates an object:

{ "onclick": fn }

That can be used to create a button like this:

m( "button", attrs.onclick( fn ) )

Object attrs.onchange( Function fn [, Object prevGenAttr] )

Creates an object:

{ 
  "onchange": m.withAttr( "value", fn ),
  "value": fn()
}

That can be used to create a bi-directional binding like this:

m( "input", attrs.onchange( fn ) )

mithril-utils's People

Watchers

N. Heise avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.