Hi,
I've been fighting with Mithril.js for some hours, and I'm asking for help 😁
I'm having an issue when m.redraw()
is called from within a Flarum Component. It appears that the call doesn't actually redraw anything most of the times, and when it does it recreates the whole Component from scratch, so all the proprerties on the component are lost.
This is a sample code for the component (full extension sample on GitHub here):
import Component from 'flarum/Component';
export default class Button extends Component {
init() {
console.log('init called');
this.count = 1;
}
view() {
console.log('redrawing. count: ' + this.count);
return m('button', { onclick: this.inc.bind(this) }, this.count);
}
inc() {
this.count++;
m.redraw();
console.log('inc called. count=' + this.count);
}
}
Combined with:
extend(TextEditor.prototype, 'controlItems', function (items) {
let button = new Button();
items.add('redraw-test', button);
});
It adds a simple button near the "Post" button in the editor. When you click that button, it increments a component property and then asks for a redraw of the component, so that the updated value is shown in the button.
The thing is that it just doesn't work. Instead of redrawing the view, it appears that the whole Component instance is recreated, and new properties are set in init().
Am I missing something? I've looked at other extensions and components in flarum/core and I don't see any particular difference from my approach...
Thank you!
EDIT: fixed sample code