VNDB fork of mithril.js
Find a file
pelonpelon 72c039c4dc allow m.withAttr callback to determine it's own this [non-breaking change]
Background:

In ES6 we now have `Object.setPrototypeOf` which makes prototype delegation an alternative to *sugary*, fake class coding patterns. This can be accomplished in ES5, but ES6 is much more elegant. Here is a basic example. `new` is never used and `this` is very easy to follow.

Working example with changes to mithril.js on lines 854, 858, 859 *only*.
http://jsbin.com/nopugo/1/edit?js,console,output

```javascript

'use strict'

const m = require('mithril')

const DataModel = {
    data: [1,2,3],
    getData(){
        console.log('getting data from server')
        return this.data
    },
    postData(val){
        console.log('sending data to server')
        this.data.push(val)
    }
}
const ViewModel = {
    filterEvenData: function() {
        return this.getData().filter((val) => (val % 2 === 0) && (+val !== 0))
    },
    addNewData: function(val){
        console.log('adding new data', val)
        this.postData(val)
    }
}

var Model = Object.setPrototypeOf(ViewModel, DataModel)

const App = {

    controller(){},

    view(ctrl){
        return m('div', [
            m('pre', [ 'EVEN NUMBERS IN DATA\n',
                this.filterEvenData().map((key,i) =>`${i+1} = ${key}\n`)                
            ]),
            m('div', 'Enter a number'),
            m('input[placeholder=number]', {

                // addNewData is called with **this** === undefined unless we pass `this` to Function.prototype.call in m.withAttr
                onchange: m.withAttr('value', App.addNewData, App),
                value: null
                }
            )
        ]) 
    }
}
Object.setPrototypeOf(App, Model)

m.mount(document.body, App)

```
2015-08-01 14:51:18 -05:00
deploy #518 fix package.json file for cdn.js 2015-03-30 14:54:20 -04:00
docs document component shorthand syntax 2015-07-23 23:43:19 -04:00
tests Add finally to promise-like object. 2015-07-24 16:57:45 -04:00
.editorconfig tweak editorconfig preferences 2015-04-22 21:00:11 -04:00
.gitattributes Avoid EOL diff horror by enforcing CRLF on commit 2015-04-26 12:53:35 +01:00
.gitignore #686 prevent redraw lock on error 2015-06-23 13:43:54 -04:00
.npmignore adds saucelabs integration to unit tests 2014-07-25 11:23:47 -04:00
.travis.yml Updated Travis CI config to run e2e tests 2014-05-28 23:46:09 +03:00
dragdrop.html rework api 2015-04-09 22:44:45 -04:00
Gruntfile.js bump version 2015-07-23 23:43:38 -04:00
guide.html rework api 2015-04-09 22:44:45 -04:00
LICENSE Initial commit 2014-03-16 18:59:39 -07:00
mithril.closure-compiler-externs.js externs for closure compiler 2014-07-21 22:48:24 -04:00
mithril.d.ts document component shorthand syntax 2015-07-23 23:43:19 -04:00
mithril.js allow m.withAttr callback to determine it's own this [non-breaking change] 2015-08-01 14:51:18 -05:00
mithril.min.js Merge remote-tracking branch 'upstream/next' into speed, unfix "fixed" version 2015-07-23 05:26:31 -04:00
mithril.min.js.map Merge remote-tracking branch 'upstream/next' into speed, unfix "fixed" version 2015-07-23 05:26:31 -04:00
modulator-test.html Remove trailing whitespace 2015-07-09 03:38:42 -04:00
mvc.html clean up 2015-04-13 08:29:03 -04:00
onerror.html clean up 2015-04-13 08:29:03 -04:00
package.json Clean up tests, resolve style differences, reduce upstream diff 2015-07-23 05:25:18 -04:00
README.md Clean up tests, resolve style differences, reduce upstream diff 2015-07-23 05:25:18 -04:00

JS.ORG Join the chat at https://gitter.im/lhorie/mithril.js Build Status

Mithril

A Javascript Framework for Building Brilliant Applications

See the website for documentation

There's also a blog and a mailing list


What is Mithril?

Mithril is a client-side MVC framework - a tool to organize code in a way that is easy to think about and to maintain.

Light-weight

  • Only 5kb gzipped, no dependencies
  • Small API, small learning curve

Robust

  • Safe-by-default templates
  • Hierarchical MVC via components

Fast

  • Virtual DOM diffing and compilable templates
  • Intelligent auto-redrawing system

Sample code

//namespace
var app = {};

//model
app.PageList = function() {
	return m.request({method: "GET", url: "pages.json"});
};

//controller
app.controller = function() {
	var pages = app.PageList();
	return {
		pages: pages,
		rotate: function() {
			pages().push(pages().shift());
		}
	}
};

//view
app.view = function(ctrl) {
	return [
		ctrl.pages().map(function(page) {
			return m("a", {href: page.url}, page.title);
		}),
		m("button", {onclick: ctrl.rotate}, "Rotate links")
	];
};


//initialize
m.module(document.getElementById("example"), app);

Learn more