如何在Angular Kendo中覆盖对话,以获取开放和关闭方法?

发布于 2025-01-27 19:47:32 字数 272 浏览 4 评论 0原文

我想拦截开放和关闭的事件以放置验证逻辑,但是在所有对话框的级别上,因此最理想的解决方案是覆盖dialogservice

“

I want to intercept the open and close events to put validation logics but that at the level of all dialogs, so the most ideal solution is to overwrite the DialogService

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2025-02-03 19:47:32

在某些情况下,最好捕获太大的系统中的开放和关闭事件,无法重构项目的每个实例。

该解决方案是在下一个URL中:

https://stackblitz.com/edit/edit/edit/angular-98yhlh-b7hiyf?file = src%2fapp%2fapp%2fpapp%2fpapp; 2f%40progress%2fkendo-angular-dialog%2fdialog%2fdialog.service.d.t.ts

import { Component, ComponentRef, Injectable } from '@angular/core';
import {
  DialogService,
  DialogRef,
  DialogCloseResult,
} from '@progress/kendo-angular-dialog';
import { DialogResult } from '@progress/kendo-angular-dialog/dialog/models/dialog-result';
import { DialogSettings } from '@progress/kendo-angular-dialog/dialog/models/dialog-settings';
import { DialogComponent } from '@progress/kendo-angular-dialog/main';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Component({
  selector: 'my-app',
  template: `
    <div class="example-wrapper">
      <div class="example-config" *ngIf="result">
        Dialog result: {{ result }}
      </div>
      <button kendoButton (click)="showConfirmation()">Please confirm</button>
    </div>

    <div kendoDialogContainer></div>
  `,
})
export class AppComponent {
  constructor(private dialogService: MyDialogService) {}

  public result;

  public showConfirmation(): void {
    const dialog: MyDialogRef = this.dialogService.open({
      title: 'Please confirm',
      content: 'Are you sure?',
      actions: [{ text: 'No' }, { text: 'Yes', themeColor: 'primary' }],
      width: 450,
      height: 200,
      minWidth: 250,
    });

    dialog.result.subscribe((result) => {
      if (result instanceof DialogCloseResult) {
        //console.log('close');
      } else {
       // console.log('action', result);
      }
      this.result = JSON.stringify(result);
    });
  }
}

@Injectable({ providedIn: 'root' })
export class MyDialogService extends DialogService {
  open(options: DialogSettings): MyDialogRef {
    console.log('is open');
    let dialogOpen  = super.open(options)
    let mydialogref:MyDialogRef = new MyDialogRef(dialogOpen.close,dialogOpen.content,dialogOpen.dialog,dialogOpen.result);
    return mydialogref;
  }
}

export class MyDialogRef implements DialogRef {
  close: Function;
  content: ComponentRef<any>;
  dialog: ComponentRef<DialogComponent>;
  private _result: Observable<DialogResult>;

  constructor(close: Function,
              content: ComponentRef<any>,
              dialog: ComponentRef<DialogComponent>,
              result: Observable<DialogResult>) {
    this.close = close;
    this.content = content;
    this.dialog = dialog;
    this._result = result;
  }


  get result(): Observable<DialogResult> {
    return this._result.pipe(map(r=>{
      console.log("return")
      return r;
    }));
  }

  set result(value: Observable<DialogResult>) {
    this._result = value;
  }
}

In some cases it is good to capture opening and closing events in systems that are too large not to refactor every instance of a project.

The solution is in the next URL :

https://stackblitz.com/edit/angular-98yhlh-b7hiyf?file=src%2Fapp%2Fapp.component.ts,node_modules%2F%40progress%2Fkendo-angular-dialog%2Fdialog%2Fdialog.service.d.ts

import { Component, ComponentRef, Injectable } from '@angular/core';
import {
  DialogService,
  DialogRef,
  DialogCloseResult,
} from '@progress/kendo-angular-dialog';
import { DialogResult } from '@progress/kendo-angular-dialog/dialog/models/dialog-result';
import { DialogSettings } from '@progress/kendo-angular-dialog/dialog/models/dialog-settings';
import { DialogComponent } from '@progress/kendo-angular-dialog/main';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Component({
  selector: 'my-app',
  template: `
    <div class="example-wrapper">
      <div class="example-config" *ngIf="result">
        Dialog result: {{ result }}
      </div>
      <button kendoButton (click)="showConfirmation()">Please confirm</button>
    </div>

    <div kendoDialogContainer></div>
  `,
})
export class AppComponent {
  constructor(private dialogService: MyDialogService) {}

  public result;

  public showConfirmation(): void {
    const dialog: MyDialogRef = this.dialogService.open({
      title: 'Please confirm',
      content: 'Are you sure?',
      actions: [{ text: 'No' }, { text: 'Yes', themeColor: 'primary' }],
      width: 450,
      height: 200,
      minWidth: 250,
    });

    dialog.result.subscribe((result) => {
      if (result instanceof DialogCloseResult) {
        //console.log('close');
      } else {
       // console.log('action', result);
      }
      this.result = JSON.stringify(result);
    });
  }
}

@Injectable({ providedIn: 'root' })
export class MyDialogService extends DialogService {
  open(options: DialogSettings): MyDialogRef {
    console.log('is open');
    let dialogOpen  = super.open(options)
    let mydialogref:MyDialogRef = new MyDialogRef(dialogOpen.close,dialogOpen.content,dialogOpen.dialog,dialogOpen.result);
    return mydialogref;
  }
}

export class MyDialogRef implements DialogRef {
  close: Function;
  content: ComponentRef<any>;
  dialog: ComponentRef<DialogComponent>;
  private _result: Observable<DialogResult>;

  constructor(close: Function,
              content: ComponentRef<any>,
              dialog: ComponentRef<DialogComponent>,
              result: Observable<DialogResult>) {
    this.close = close;
    this.content = content;
    this.dialog = dialog;
    this._result = result;
  }


  get result(): Observable<DialogResult> {
    return this._result.pipe(map(r=>{
      console.log("return")
      return r;
    }));
  }

  set result(value: Observable<DialogResult>) {
    this._result = value;
  }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文