Skip to content

Commit

Permalink
fix(acl): fix invalid control when acl has changed
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Feb 27, 2018
1 parent 41dcdcc commit 6944339
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion scaffold
16 changes: 13 additions & 3 deletions src/core/acl/directives/acl.directive.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Directive, Input, ElementRef, Renderer2 } from '@angular/core';
import { Directive, Input, ElementRef, Renderer2, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import { ACLService } from '../services/acl.service';
import { ACLType } from '../services/acl.type';

@Directive({
selector: '[acl]'
})
export class ACLDirective {
export class ACLDirective implements OnDestroy {
private _value: any;
private change$: Subscription;

@Input('acl')
set acl(value: number | number[] | string | string[] | ACLType) {
Expand All @@ -28,7 +31,14 @@ export class ACLDirective {
} else {
this.renderer.addClass(el, CLS);
}
this._value = value;
}

constructor(private el: ElementRef, private renderer: Renderer2, private srv: ACLService) {}
constructor(private el: ElementRef, private renderer: Renderer2, private srv: ACLService) {
this.change$ = <any>this.srv.change.subscribe(() => this.set(this._value));
}

ngOnDestroy(): void {
if (this.change$) this.change$.unsubscribe();
}
}
14 changes: 14 additions & 0 deletions src/core/acl/services/acl.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import { ACLType, ACLCanType } from './acl.type';

/**
Expand All @@ -10,6 +12,12 @@ export class ACLService {
private roles: string[] = [];
private abilities: (number | string)[] = [];
private full = false;
private aclChange: BehaviorSubject<ACLType | boolean> = new BehaviorSubject<ACLType | boolean>(null);

/** ACL变更通知 */
get change(): Observable<ACLType | boolean> {
return this.aclChange.asObservable();
}

/** 获取所有数据 */
get data() {
Expand Down Expand Up @@ -41,6 +49,7 @@ export class ACLService {
this.abilities = [];
this.roles = [];
this.add(value);
this.aclChange.next(value);
}

/**
Expand All @@ -50,6 +59,7 @@ export class ACLService {
*/
setFull(val: boolean) {
this.full = val;
this.aclChange.next(val);
}

/**
Expand Down Expand Up @@ -95,6 +105,7 @@ export class ACLService {
this.roles.push(val);
}
}
this.aclChange.next(this.data);
}

/**
Expand All @@ -108,6 +119,7 @@ export class ACLService {
this.abilities.push(val);
}
}
this.aclChange.next(this.data);
}

/**
Expand All @@ -122,6 +134,7 @@ export class ACLService {
this.roles.splice(idx, 1);
}
}
this.aclChange.next(this.data);
}

/**
Expand All @@ -136,6 +149,7 @@ export class ACLService {
this.abilities.splice(idx, 1);
}
}
this.aclChange.next(this.data);
}

/**
Expand Down

0 comments on commit 6944339

Please sign in to comment.