discourse/app/assets/javascripts/discourse-common/lib/buffered-render.js.es6

44 lines
967 B
Plaintext
Raw Normal View History

// Ember 2.0 removes buffered rendering, but we can still implement it ourselves.
// In the long term we'll want to remove this.
const Mixin = {
_customRender() {
2018-06-15 23:03:24 +08:00
if (!this.element || this.isDestroying || this.isDestroyed) {
return;
}
const buffer = [];
this.buildBuffer(buffer);
2018-06-15 23:03:24 +08:00
this.element.innerHTML = buffer.join("");
},
rerenderBuffer() {
2018-06-15 23:03:24 +08:00
Ember.run.scheduleOnce("render", this, this._customRender);
}
};
export function bufferedRender(obj) {
if (!obj.buildBuffer) {
2018-06-15 23:03:24 +08:00
Ember.warn("Missing `buildBuffer` method");
return obj;
}
2018-06-15 23:03:24 +08:00
const caller = {};
2016-11-26 04:38:45 +08:00
caller.didRender = function() {
this._super();
this._customRender();
};
const triggers = obj.rerenderTriggers;
if (triggers) {
caller.init = function() {
this._super();
triggers.forEach(k => this.addObserver(k, this.rerenderBuffer));
};
}
delete obj.rerenderTriggers;
return Ember.Mixin.create(Mixin, caller, obj);
}