marko/components/registry.js
Patrick Steele-Idem 261528e5db Fixes #599 - [v4] Bug: when rerendering, custom events may not be attached correctly when reusing components
The fix is to always reattach custom events even if the UI component is preserved.
2017-02-24 12:19:05 -07:00

73 lines
2.0 KiB
JavaScript

'use strict';
const extend = require('raptor-util/extend');
const SERVER_WIDGET_KEY = Symbol();
function createServerComponentClass(renderingLogic) {
class ServerComponent {
constructor(id, input, out, typeName, customEvents, scope) {
this.id = id;
this.$__customEvents = customEvents;
this.$__scope = scope;
this.$__updatedInput = undefined;
this.$__input = undefined;
this.$__state = undefined;
this.typeName = typeName;
if (this.onCreate) {
this.onCreate(input, out);
}
if (this.onInput) {
var updatedInput = this.onInput(input, out) || input;
if (this.$__input === undefined) {
this.$__input = updatedInput;
}
this.$__updatedInput = updatedInput;
} else {
this.$__input = this.$__updatedInput = input;
}
if (this.onRender) {
this.onRender(out);
}
}
set input(newInput) {
this.$__input = newInput;
}
get input() {
return this.$__input;
}
set state(newState) {
this.$__state = newState;
}
get state() {
return this.$__state;
}
get $__rawState() {
return this.$__state;
}
}
extend(ServerComponent.prototype, renderingLogic);
return ServerComponent;
}
function createComponent(renderingLogic, id, input, out, typeName, customEvents, scope) {
var ServerComponent = renderingLogic[SERVER_WIDGET_KEY];
if (!ServerComponent) {
ServerComponent = renderingLogic[SERVER_WIDGET_KEY] = createServerComponentClass(renderingLogic);
}
var component = new ServerComponent(id, input, out, typeName, customEvents, scope);
return component;
}
exports.$__isServer = true;
exports.$__createComponent = createComponent;