From df385b7df2955b6bde900019e0d0636f7025a8cb Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Wed, 26 Aug 2015 16:22:29 +0930 Subject: [PATCH] Monkey patch mithril so we can use JSX component syntax instead of Alert.component({foo: 'bar'}) --- js/lib/App.js | 3 +++ js/lib/utils/patchMithril.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 js/lib/utils/patchMithril.js diff --git a/js/lib/App.js b/js/lib/App.js index 0954b2016..1e00cd992 100644 --- a/js/lib/App.js +++ b/js/lib/App.js @@ -2,6 +2,7 @@ import ItemList from 'flarum/utils/ItemList'; import Alert from 'flarum/components/Alert'; import Translator from 'flarum/Translator'; import extract from 'flarum/utils/extract'; +import patchMithril from 'flarum/utils/patchMithril'; /** * The `App` class provides a container for an application, as well as various @@ -9,6 +10,8 @@ import extract from 'flarum/utils/extract'; */ export default class App { constructor() { + patchMithril(window); + /** * The forum model for this application. * diff --git a/js/lib/utils/patchMithril.js b/js/lib/utils/patchMithril.js new file mode 100644 index 000000000..a92f7467d --- /dev/null +++ b/js/lib/utils/patchMithril.js @@ -0,0 +1,17 @@ +import Component from '../Component'; + +export default function patchMithril(global) { + const mo = global.m; + + const m = function(comp, ...args) { + if (comp.prototype && comp.prototype instanceof Component) { + return comp.component(...args); + } + + return mo.apply(this, arguments); + } + + Object.keys(mo).forEach(key => m[key] = mo[key]); + + global.m = m; +}