npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mithril-component

v1.0.2

Published

React style components for Mithril.js

Downloads

21

Readme

Mithril Component

React style components for Mithril.js

version License Build Status Downloads Code Climate Coverage Status Dependencies

Install

Usage

Node.js / Browserify

// Include mithril
var m = require('mithril')

// Pass mithril to the component system.
// Only required to overload once, subsequent overloads will
// return the same instance
require('mithril-component')(m)

Browser

<script src="path/to/mithril.js" type="text/javascript"></script>
<script src="path/to/mithril.component.js" type="text/javascript"></script>

Documentation

m.createComponent(Object specification)

Creates and returns a component class constructor which takes two arguments:

Function ComponentClassConstructor (Object props, Array Children)

Component specification requires a view method which returns a mithril node element. Components specification may also contain a controller method which is ran when the class is created, values returned will be set on this.state, to learn more see Specification Object.

Specification Object

  • specification.controller(Object props, Array children)

    Invoked immediately when used within another mithril element as a child.

    Using Promises

    Supports returning A+ promises, when a promise is returned inside of the controller method, the result is caught by a wrapper which then attaches itself to the then and catch (should it exist) methods. On success the promise result data replaces component.state. On error component.onControllerError is invoked with the error passed as the first argument.

  • specification.view() Render method, should return a mithril element.

  • specification.onControllerError(Object error) Invoked when a returned promise fails.

  • specification.onUnload() Invoked when component is unloaded.

Component Class Object

When ComponentClassConstructor is invoked the resulting object contains the following properties.

  • component.state Component class internal state
  • component.props Component class properties passed from parent
  • component.setState(Object State) Sets component.state and invokes m.redraw(true)
  • component.controller(Object props, Array children)
  • component.view() Render method, should return a mithril element.
  • component.onControllerError(Object error) Invoked when a returned promise fails.
  • component.onUnload() Invoked when component is unloaded.

Example Usage

  1. Basic usage w/ Children props ( React Equivalent )
var CheckLink = m.createComponent({
  view: function () {
    this.props.children.unshift('√ ')
    return m('a', this.props.attr || {}, this.props.children);
  }
})

m.render(document.body, m('div.component-holder', [
  CheckLink({
    attr: { href: '#' },
  }, 'Check, Check it out')
]))
  1. Controller usage
var TimerComponent = m.createComponent({
  controller: function () {
    return this.startClock()
  },

  startClock: function () {
    var component = this
    this.state.seconds = 0
    this.state.clock = setInterval(function () {
      component.state.seconds++
      component.setState(component.state)
    }, 1000)

    return this.state
  },

  stopClock: function () {
    clearInterval(this.state.clock)
  },

  onUnload: function () {
    this.stopClock()
  },

  view: function (ctrl) {
    return m('div.timer-component', this.state.seconds);
  }
})

m.mount(document.body, TimerComponent())

License

Licensed under The MIT License.