Browse Source

refactor(core): add resetDefaultComponent method to replaceable-template directive

#2404
pull/2522/head
mehmet-erim 7 years ago
parent
commit
9e62f63341
  1. 50
      npm/ng-packs/packages/core/src/lib/directives/replaceable-template.directive.ts

50
npm/ng-packs/packages/core/src/lib/directives/replaceable-template.directive.ts

@ -50,10 +50,7 @@ export class ReplaceableTemplateDirective implements OnInit, OnDestroy, OnChange
) { ) {
this.context = { this.context = {
initTemplate: ref => { initTemplate: ref => {
Object.keys(this.defaultComponentSubscriptions).forEach(key => { this.resetDefaultComponent();
this.defaultComponentSubscriptions[key].unsubscribe();
});
this.defaultComponentSubscriptions = {} as ABP.Dictionary<Subscription>;
this.defaultComponentRef = ref; this.defaultComponentRef = ref;
this.setDefaultComponentInputs(); this.setDefaultComponentInputs();
}, },
@ -73,6 +70,9 @@ export class ReplaceableTemplateDirective implements OnInit, OnDestroy, OnChange
.subscribe((res = {} as ReplaceableComponents.ReplaceableComponent) => { .subscribe((res = {} as ReplaceableComponents.ReplaceableComponent) => {
this.vcRef.clear(); this.vcRef.clear();
this.externalComponent = res.component; this.externalComponent = res.component;
if (this.defaultComponentRef) {
this.resetDefaultComponent();
}
if (res.component) { if (res.component) {
this.setProvidedData(); this.setProvidedData();
@ -102,19 +102,27 @@ export class ReplaceableTemplateDirective implements OnInit, OnDestroy, OnChange
ngOnDestroy() {} ngOnDestroy() {}
setDefaultComponentInputs() { setDefaultComponentInputs() {
if (!this.defaultComponentRef || !this.data.inputs) return; if (!this.defaultComponentRef || (!this.data.inputs && !this.data.outputs)) return;
for (const key in this.data.inputs) { if (this.data.inputs) {
if (this.data.inputs.hasOwnProperty(key)) { for (const key in this.data.inputs) {
if (!compare(this.defaultComponentRef[key], this.data.inputs[key].value)) { if (this.data.inputs.hasOwnProperty(key)) {
this.defaultComponentRef[key] = this.data.inputs[key].value; if (!compare(this.defaultComponentRef[key], this.data.inputs[key].value)) {
this.defaultComponentRef[key] = this.data.inputs[key].value;
if (this.data.inputs[key].twoWay && !this.defaultComponentSubscriptions[key]) { }
this.defaultComponentSubscriptions[key] = this.defaultComponentRef[ }
`${key}Change` }
].subscribe(value => { }
this.data.outputs[`${key}Change`](value);
}); if (this.data.outputs) {
for (const key in this.data.outputs) {
if (this.data.outputs.hasOwnProperty(key)) {
if (!this.defaultComponentSubscriptions[key]) {
this.defaultComponentSubscriptions[key] = this.defaultComponentRef[key].subscribe(
value => {
this.data.outputs[key](value);
},
);
} }
} }
} }
@ -145,4 +153,12 @@ export class ReplaceableTemplateDirective implements OnInit, OnDestroy, OnChange
), ),
}); });
} }
resetDefaultComponent() {
Object.keys(this.defaultComponentSubscriptions).forEach(key => {
this.defaultComponentSubscriptions[key].unsubscribe();
});
this.defaultComponentSubscriptions = {} as ABP.Dictionary<Subscription>;
this.defaultComponentRef = null;
}
} }

Loading…
Cancel
Save