mithril.js: Components don't work with ES6 classes.

Since ES6 classes need to be called with new and this needs to be an instance of the class, components will not work with a class. The issue stems from here: https://github.com/lhorie/mithril.js/blob/next/mithril.js#L554

Example (using babel-compiled class): https://jsfiddle.net/1prjtv78/

A work around is to wrap the component controller like:

var Component = {
   controller: function () { return new ComponentController(...arguments); }.
   view: View
};

Which transpiles to something equally ugly:

var _bind = Function.prototype.bind;
var Component = {
   controller: function () { return new (_bind.apply(ComponentController, [null].concat(arguments)))(); }.
   view: View
};

Example of working: https://jsfiddle.net/1prjtv78/1/

Perhaps the parametize.controller function can do something similar to the transpiled code.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 72 (61 by maintainers)

Commits related to this issue

Most upvoted comments

plz, plz make the use of classes optional. I don’t want to be forced to use them. I find them cumbersome and ugly. The code is IMHO much easier to read without them.

Working on this now for the rewrite.

@pygy I was hoping for something that doesn’t require a base class, but in the absence of such a solution, that’s actually a pretty elegant alternative