Migrate Discourse Polls to use vdom instead of embedded ember

This commit is contained in:
Robin Ward
2016-12-07 15:48:47 -05:00
parent 846597f563
commit f07443b488
23 changed files with 702 additions and 620 deletions

View File

@@ -110,6 +110,7 @@ export default Ember.Component.extend({
const opts = { model: this.get('model') };
const newTree = new this._widgetClass(args, this.register, opts);
newTree._rerenderable = this;
newTree._emberView = this;
const patches = diff(this._tree || this._rootNode, newTree);

View File

@@ -0,0 +1,43 @@
import { diff, patch } from 'virtual-dom';
import { queryRegistry } from 'discourse/widgets/widget';
export default class WidgetGlue {
constructor(name, register, attrs) {
this._tree = null;
this._rootNode = null;
this.register = register;
this.attrs = attrs;
this._timeout = null;
this._widgetClass = queryRegistry(name) || this.register.lookupFactory(`widget:${name}`);
if (!this._widgetClass) {
console.error(`Error: Could not find widget: ${name}`);
}
}
appendTo(elem) {
this._rootNode = elem;
this.queueRerender();
}
queueRerender() {
this._timeout = Ember.run.scheduleOnce('render', this, this.rerenderWidget);
}
rerenderWidget() {
Ember.run.cancel(this._timeout);
const newTree = new this._widgetClass(this.attrs, this.register);
const patches = diff(this._tree || this._rootNode, newTree);
newTree._rerenderable = this;
this._rootNode = patch(this._rootNode, patches);
this._tree = newTree;
}
cleanUp() {
Ember.run.cancel(this._timeout);
}
}

View File

@@ -246,10 +246,11 @@ export default class Widget {
keyDirty(widget.key);
}
const emberView = widget._emberView;
if (emberView) {
return emberView.queueRerender();
const rerenderable = widget._rerenderable;
if (rerenderable) {
return rerenderable.queueRerender();
}
widget = widget.parentWidget;
}
}